From 5f764ab8f5e28ac2744f8cbff648087bcf70b767 Mon Sep 17 00:00:00 2001 From: alexandre patelli Date: Sat, 10 Mar 2018 18:25:20 +0100 Subject: [PATCH 1/4] Media Button Play/Pause, Previous and Next in Background Player --- app/src/main/AndroidManifest.xml | 6 +++ .../newpipe/player/BackgroundPlayer.java | 47 +++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1be8c1f2c..18b3222a0 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -43,6 +43,12 @@ android:launchMode="singleTask" android:label="@string/title_activity_background_player"/> + + + + + + Date: Sun, 11 Mar 2018 15:09:11 +0100 Subject: [PATCH 2/4] Direct use of AudioManager from AudioReactor --- .../java/org/schabi/newpipe/player/BackgroundPlayer.java | 7 ++----- .../org/schabi/newpipe/player/helper/AudioReactor.java | 9 +++++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java b/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java index 48249d876..bc28116cd 100644 --- a/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java @@ -28,7 +28,6 @@ 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; @@ -82,7 +81,6 @@ public final class BackgroundPlayer extends Service { private BasePlayerImpl basePlayerImpl; private LockManager lockManager; - private AudioManager mAudioManager; private ComponentName mReceiverComponent; /*////////////////////////////////////////////////////////////////////////// @@ -122,9 +120,8 @@ 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); + basePlayerImpl.audioReactor.registerMediaButtonEventReceiver(mReceiverComponent); } @Override @@ -163,7 +160,7 @@ public final class BackgroundPlayer extends Service { basePlayerImpl = null; lockManager = null; - mAudioManager.unregisterMediaButtonEventReceiver(mReceiverComponent); + basePlayerImpl.audioReactor.unregisterMediaButtonEventReceiver(mReceiverComponent); stopForeground(true); stopSelf(); diff --git a/app/src/main/java/org/schabi/newpipe/player/helper/AudioReactor.java b/app/src/main/java/org/schabi/newpipe/player/helper/AudioReactor.java index 2c85cfc34..0e6642eed 100644 --- a/app/src/main/java/org/schabi/newpipe/player/helper/AudioReactor.java +++ b/app/src/main/java/org/schabi/newpipe/player/helper/AudioReactor.java @@ -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,14 @@ public class AudioReactor implements AudioManager.OnAudioFocusChangeListener, Au return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O; } + public void registerMediaButtonEventReceiver(ComponentName componentName) { + audioManager.registerMediaButtonEventReceiver(componentName); + } + + public void unregisterMediaButtonEventReceiver(ComponentName componentName) { + audioManager.unregisterMediaButtonEventReceiver(componentName); + } + /*////////////////////////////////////////////////////////////////////////// // AudioFocus //////////////////////////////////////////////////////////////////////////*/ From 36457400e708dc284e2bbb219b472f8baf795aee Mon Sep 17 00:00:00 2001 From: alexandre patelli Date: Sun, 11 Mar 2018 19:23:00 +0100 Subject: [PATCH 3/4] Review Fixes --- .../java/org/schabi/newpipe/player/BackgroundPlayer.java | 7 +++++-- .../org/schabi/newpipe/player/helper/AudioReactor.java | 8 ++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java b/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java index bc28116cd..3e9a63886 100644 --- a/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java @@ -152,6 +152,7 @@ public final class BackgroundPlayer extends Service { lockManager.releaseWifiAndCpu(); } if (basePlayerImpl != null) { + basePlayerImpl.audioReactor.unregisterMediaButtonEventReceiver(mReceiverComponent); basePlayerImpl.stopActivityBinding(); basePlayerImpl.destroy(); } @@ -160,8 +161,6 @@ public final class BackgroundPlayer extends Service { basePlayerImpl = null; lockManager = null; - basePlayerImpl.audioReactor.unregisterMediaButtonEventReceiver(mReceiverComponent); - stopForeground(true); stopSelf(); } @@ -594,6 +593,10 @@ public final class BackgroundPlayer extends Service { 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); + } 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 { diff --git a/app/src/main/java/org/schabi/newpipe/player/helper/AudioReactor.java b/app/src/main/java/org/schabi/newpipe/player/helper/AudioReactor.java index 0e6642eed..df30c3e79 100644 --- a/app/src/main/java/org/schabi/newpipe/player/helper/AudioReactor.java +++ b/app/src/main/java/org/schabi/newpipe/player/helper/AudioReactor.java @@ -88,10 +88,18 @@ public class AudioReactor implements AudioManager.OnAudioFocusChangeListener, Au } 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); } From 24f2999669a8db7a92a69f1325836d160502ed90 Mon Sep 17 00:00:00 2001 From: alexandre patelli Date: Tue, 13 Mar 2018 22:57:59 +0100 Subject: [PATCH 4/4] Handling play/pause button from different headsets --- .../main/java/org/schabi/newpipe/player/BackgroundPlayer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java b/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java index 3e9a63886..06b62f46f 100644 --- a/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java @@ -591,7 +591,7 @@ public final class BackgroundPlayer extends Service { 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) { + } 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);