-Designated background player as default media button receiver.

-Fixed media button intent causing illegal state exception when sent from external apps.
This commit is contained in:
John Zhen Mo 2018-04-02 22:11:53 -07:00
parent 238bff1fee
commit 74199c8624
3 changed files with 20 additions and 10 deletions

View File

@ -42,7 +42,11 @@
<service <service
android:name=".player.BackgroundPlayer" android:name=".player.BackgroundPlayer"
android:exported="false"/> android:exported="false">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</service>
<activity <activity
android:name=".player.BackgroundPlayerActivity" android:name=".player.BackgroundPlayerActivity"

View File

@ -118,8 +118,12 @@ public final class BackgroundPlayer extends Service {
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
if (DEBUG) Log.d(TAG, "onStartCommand() called with: intent = [" + intent + "], flags = [" + flags + "], startId = [" + startId + "]"); if (DEBUG) Log.d(TAG, "onStartCommand() called with: intent = [" + intent +
"], flags = [" + flags + "], startId = [" + startId + "]");
basePlayerImpl.handleIntent(intent); basePlayerImpl.handleIntent(intent);
if (basePlayerImpl.mediaSessionManager != null) {
basePlayerImpl.mediaSessionManager.handleMediaButtonIntent(intent);
}
return START_NOT_STICKY; return START_NOT_STICKY;
} }

View File

@ -1,8 +1,12 @@
package org.schabi.newpipe.player.helper; package org.schabi.newpipe.player.helper;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.media.session.MediaButtonReceiver;
import android.support.v4.media.session.MediaSessionCompat; import android.support.v4.media.session.MediaSessionCompat;
import android.view.KeyEvent;
import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector; import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector;
@ -15,8 +19,8 @@ import org.schabi.newpipe.player.mediasession.PlayQueuePlaybackController;
public class MediaSessionManager { public class MediaSessionManager {
private static final String TAG = "MediaSessionManager"; private static final String TAG = "MediaSessionManager";
private final MediaSessionCompat mediaSession; @NonNull private final MediaSessionCompat mediaSession;
private final MediaSessionConnector sessionConnector; @NonNull private final MediaSessionConnector sessionConnector;
public MediaSessionManager(@NonNull final Context context, public MediaSessionManager(@NonNull final Context context,
@NonNull final Player player, @NonNull final Player player,
@ -28,11 +32,9 @@ public class MediaSessionManager {
this.sessionConnector.setPlayer(player, new DummyPlaybackPreparer()); this.sessionConnector.setPlayer(player, new DummyPlaybackPreparer());
} }
public MediaSessionCompat getMediaSession() { @Nullable
return mediaSession; @SuppressWarnings("UnusedReturnValue")
} public KeyEvent handleMediaButtonIntent(final Intent intent) {
return MediaButtonReceiver.handleIntent(mediaSession, intent);
public MediaSessionConnector getSessionConnector() {
return sessionConnector;
} }
} }