mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2024-12-24 09:00:31 +00:00
Media Button Play/Pause, Previous and Next in Background Player
This commit is contained in:
parent
9c9b6bc0d6
commit
5f764ab8f5
@ -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"
|
||||
|
@ -22,10 +22,13 @@ 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;
|
||||
import android.graphics.Bitmap;
|
||||
import android.media.AudioManager;
|
||||
import android.os.Build;
|
||||
import android.os.IBinder;
|
||||
import android.support.annotation.IntRange;
|
||||
@ -33,6 +36,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 +81,10 @@ public final class BackgroundPlayer extends Service {
|
||||
|
||||
private BasePlayerImpl basePlayerImpl;
|
||||
private LockManager lockManager;
|
||||
|
||||
private AudioManager mAudioManager;
|
||||
private ComponentName mReceiverComponent;
|
||||
|
||||
/*//////////////////////////////////////////////////////////////////////////
|
||||
// Service-Activity Binder
|
||||
//////////////////////////////////////////////////////////////////////////*/
|
||||
@ -113,6 +121,10 @@ public final class BackgroundPlayer extends Service {
|
||||
|
||||
mBinder = new PlayerServiceBinder(basePlayerImpl);
|
||||
shouldUpdateOnProgress = true;
|
||||
|
||||
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
|
||||
mReceiverComponent = new ComponentName(this, MediaButtonReceiver.class);
|
||||
mAudioManager.registerMediaButtonEventReceiver(mReceiverComponent);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -151,6 +163,8 @@ public final class BackgroundPlayer extends Service {
|
||||
basePlayerImpl = null;
|
||||
lockManager = null;
|
||||
|
||||
mAudioManager.unregisterMediaButtonEventReceiver(mReceiverComponent);
|
||||
|
||||
stopForeground(true);
|
||||
stopSelf();
|
||||
}
|
||||
@ -563,4 +577,37 @@ 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) {
|
||||
pendingIntent = PendingIntent.getBroadcast(context, NOTIFICATION_ID, new Intent(ACTION_PLAY_PAUSE), PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
}
|
||||
if (pendingIntent != null) {
|
||||
try {
|
||||
pendingIntent.send();
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Error Sending intent MediaButtonReceiver", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user