Merge branch 'media_buttons' of https://github.com/alexandrepa/NewPipe into media

This commit is contained in:
Christian Schabesberger 2018-03-14 18:27:17 +01:00
commit ffc5ad5ce7
3 changed files with 70 additions and 0 deletions

View File

@ -43,6 +43,12 @@
android:launchMode="singleTask"
android:label="@string/title_activity_background_player"/>
<receiver android:name="org.schabi.newpipe.player.BackgroundPlayer$MediaButtonReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
<activity
android:name=".player.PopupVideoPlayerActivity"
android:launchMode="singleTask"

View File

@ -22,6 +22,8 @@ package org.schabi.newpipe.player;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
@ -33,6 +35,7 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RemoteViews;
@ -77,6 +80,9 @@ public final class BackgroundPlayer extends Service {
private BasePlayerImpl basePlayerImpl;
private LockManager lockManager;
private ComponentName mReceiverComponent;
/*//////////////////////////////////////////////////////////////////////////
// Service-Activity Binder
//////////////////////////////////////////////////////////////////////////*/
@ -113,6 +119,9 @@ public final class BackgroundPlayer extends Service {
mBinder = new PlayerServiceBinder(basePlayerImpl);
shouldUpdateOnProgress = true;
mReceiverComponent = new ComponentName(this, MediaButtonReceiver.class);
basePlayerImpl.audioReactor.registerMediaButtonEventReceiver(mReceiverComponent);
}
@Override
@ -143,6 +152,7 @@ public final class BackgroundPlayer extends Service {
lockManager.releaseWifiAndCpu();
}
if (basePlayerImpl != null) {
basePlayerImpl.audioReactor.unregisterMediaButtonEventReceiver(mReceiverComponent);
basePlayerImpl.stopActivityBinding();
basePlayerImpl.destroy();
}
@ -563,4 +573,41 @@ public final class BackgroundPlayer extends Service {
lockManager.releaseWifiAndCpu();
}
}
public static class MediaButtonReceiver extends BroadcastReceiver {
public MediaButtonReceiver() {
super();
}
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event.getAction() == KeyEvent.ACTION_UP) {
int keycode = event.getKeyCode();
PendingIntent pendingIntent = null;
if (keycode == KeyEvent.KEYCODE_MEDIA_NEXT) {
pendingIntent = PendingIntent.getBroadcast(context, NOTIFICATION_ID, new Intent(ACTION_PLAY_NEXT), PendingIntent.FLAG_UPDATE_CURRENT);
} else if (keycode == KeyEvent.KEYCODE_MEDIA_PREVIOUS) {
pendingIntent = PendingIntent.getBroadcast(context, NOTIFICATION_ID, new Intent(ACTION_PLAY_PREVIOUS), PendingIntent.FLAG_UPDATE_CURRENT);
} else if (keycode == KeyEvent.KEYCODE_HEADSETHOOK || keycode == KeyEvent.KEYCODE_MEDIA_PAUSE || keycode == KeyEvent.KEYCODE_MEDIA_PLAY) {
pendingIntent = PendingIntent.getBroadcast(context, NOTIFICATION_ID, new Intent(ACTION_PLAY_PAUSE), PendingIntent.FLAG_UPDATE_CURRENT);
} else if (keycode == KeyEvent.KEYCODE_MEDIA_FAST_FORWARD) {
pendingIntent = PendingIntent.getBroadcast(context, NOTIFICATION_ID, new Intent(ACTION_FAST_FORWARD), PendingIntent.FLAG_UPDATE_CURRENT);
} else if (keycode == KeyEvent.KEYCODE_MEDIA_REWIND) {
pendingIntent = PendingIntent.getBroadcast(context, NOTIFICATION_ID, new Intent(ACTION_FAST_REWIND), PendingIntent.FLAG_UPDATE_CURRENT);
}
if (pendingIntent != null) {
try {
pendingIntent.send();
} catch (Exception e) {
Log.e(TAG, "Error Sending intent MediaButtonReceiver", e);
}
}
}
}
}
}
}

View File

@ -3,6 +3,7 @@ package org.schabi.newpipe.player.helper;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.AudioFocusRequest;
@ -86,6 +87,22 @@ public class AudioReactor implements AudioManager.OnAudioFocusChangeListener, Au
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
}
public void registerMediaButtonEventReceiver(ComponentName componentName) {
if (android.os.Build.VERSION.SDK_INT > 27) {
Log.e(TAG, "registerMediaButtonEventReceiver has been deprecated and maybe not supported anymore.");
return;
}
audioManager.registerMediaButtonEventReceiver(componentName);
}
public void unregisterMediaButtonEventReceiver(ComponentName componentName) {
if (android.os.Build.VERSION.SDK_INT > 27) {
Log.e(TAG, "unregisterMediaButtonEventReceiver has been deprecated and maybe not supported anymore.");
return;
}
audioManager.unregisterMediaButtonEventReceiver(componentName);
}
/*//////////////////////////////////////////////////////////////////////////
// AudioFocus
//////////////////////////////////////////////////////////////////////////*/