Merge "Update for MediaSessionCompat support" into lmp-mr1-dev

This commit is contained in:
RoboErik
2014-12-03 19:06:05 +00:00
committed by Android (Google) Code Review
3 changed files with 52 additions and 64 deletions

View File

@@ -16,7 +16,10 @@
package com.example.android.supportv7.media; package com.example.android.supportv7.media;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.support.v4.media.MediaMetadataCompat; import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaSessionCompat; import android.support.v4.media.session.MediaSessionCompat;
@@ -43,7 +46,6 @@ public abstract class Player {
protected Callback mCallback; protected Callback mCallback;
protected MediaSessionCompat mMediaSession; protected MediaSessionCompat mMediaSession;
protected MediaSessionCallback mSessionCallback;
public abstract boolean isRemotePlayback(); public abstract boolean isRemotePlayback();
public abstract boolean isQueuingSupported(); public abstract boolean isQueuingSupported();
@@ -76,7 +78,7 @@ public abstract class Player {
mCallback = callback; mCallback = callback;
} }
public static Player create(Context context, RouteInfo route) { public static Player create(Context context, RouteInfo route, MediaSessionCompat session) {
Player player; Player player;
if (route != null && route.supportsControlCategory( if (route != null && route.supportsControlCategory(
MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)) { MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)) {
@@ -86,7 +88,7 @@ public abstract class Player {
} else { } else {
player = new LocalPlayer.OverlayPlayer(context); player = new LocalPlayer.OverlayPlayer(context);
} }
player.initMediaSession(context); player.initMediaSession(session);
player.connect(route); player.connect(route);
return player; return player;
} }
@@ -96,6 +98,9 @@ public abstract class Player {
} }
protected void updateMetadata() { protected void updateMetadata() {
if (mMediaSession == null) {
return;
}
MediaMetadataCompat.Builder bob = new MediaMetadataCompat.Builder(); MediaMetadataCompat.Builder bob = new MediaMetadataCompat.Builder();
bob.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, getDescription()); bob.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, getDescription());
bob.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE, "Subtitle of the thing"); bob.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE, "Subtitle of the thing");
@@ -106,6 +111,9 @@ public abstract class Player {
} }
protected void publishState(int state) { protected void publishState(int state) {
if (mMediaSession == null) {
return;
}
PlaybackStateCompat.Builder bob = new PlaybackStateCompat.Builder(); PlaybackStateCompat.Builder bob = new PlaybackStateCompat.Builder();
bob.setActions(PLAYBACK_ACTIONS); bob.setActions(PLAYBACK_ACTIONS);
switch (state) { switch (state) {
@@ -130,26 +138,11 @@ public abstract class Player {
} }
} }
private void initMediaSession(Context context) { private void initMediaSession(MediaSessionCompat session) {
mSessionCallback = new MediaSessionCallback(); mMediaSession = session;
mMediaSession = new MediaSessionCompat(context, "Support7Demos");
mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
| MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mMediaSession.setCallback(mSessionCallback);
updateMetadata(); updateMetadata();
} }
private final class MediaSessionCallback extends MediaSessionCompat.Callback {
@Override
public void onPlay() {
resume();
}
@Override
public void onPause() {
pause();
}
}
public interface Callback { public interface Callback {
void onError(); void onError();

View File

@@ -248,7 +248,7 @@ final class SampleMediaRouteProvider extends MediaRouteProvider {
public SampleRouteController(String routeId) { public SampleRouteController(String routeId) {
mRouteId = routeId; mRouteId = routeId;
mPlayer = Player.create(getContext(), null); mPlayer = Player.create(getContext(), null, null);
mSessionManager.setPlayer(mPlayer); mSessionManager.setPlayer(mPlayer);
mSessionManager.setCallback(new SessionManager.Callback() { mSessionManager.setCallback(new SessionManager.Callback() {
@Override @Override

View File

@@ -35,6 +35,7 @@ import android.os.Handler;
import android.os.Bundle; import android.os.Bundle;
import android.os.SystemClock; import android.os.SystemClock;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.view.MenuItemCompat; import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.MediaRouteActionProvider; import android.support.v7.app.MediaRouteActionProvider;
@@ -132,19 +133,18 @@ public class SampleMediaRouterActivity extends ActionBarActivity {
public void onRouteSelected(MediaRouter router, RouteInfo route) { public void onRouteSelected(MediaRouter router, RouteInfo route) {
Log.d(TAG, "onRouteSelected: route=" + route); Log.d(TAG, "onRouteSelected: route=" + route);
mPlayer = Player.create(SampleMediaRouterActivity.this, route); mPlayer = Player.create(SampleMediaRouterActivity.this, route, mMediaSession);
mPlayer.updatePresentation(); mPlayer.updatePresentation();
mSessionManager.setPlayer(mPlayer); mSessionManager.setPlayer(mPlayer);
mSessionManager.unsuspend(); mSessionManager.unsuspend();
registerRCC();
updateUi(); updateUi();
} }
@Override @Override
public void onRouteUnselected(MediaRouter router, RouteInfo route) { public void onRouteUnselected(MediaRouter router, RouteInfo route) {
Log.d(TAG, "onRouteUnselected: route=" + route); Log.d(TAG, "onRouteUnselected: route=" + route);
unregisterRCC(); mMediaSession.setActive(false);
PlaylistItem item = getCheckedPlaylistItem(); PlaylistItem item = getCheckedPlaylistItem();
if (item != null) { if (item != null) {
@@ -184,7 +184,7 @@ public class SampleMediaRouterActivity extends ActionBarActivity {
} }
}; };
private RemoteControlClient mRemoteControlClient; private MediaSessionCompat mMediaSession;
private ComponentName mEventReceiver; private ComponentName mEventReceiver;
private AudioManager mAudioManager; private AudioManager mAudioManager;
private PendingIntent mMediaPendingIntent; private PendingIntent mMediaPendingIntent;
@@ -368,12 +368,12 @@ public class SampleMediaRouterActivity extends ActionBarActivity {
mMediaPendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0); mMediaPendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0);
// Create and register the remote control client // Create and register the remote control client
registerRCC(); createMediaSession();
mMediaRouter.setMediaSessionCompat(mMediaSession);
// Set up playback manager and player // Set up playback manager and player
mPlayer = Player.create(SampleMediaRouterActivity.this, mPlayer = Player.create(SampleMediaRouterActivity.this,
mMediaRouter.getSelectedRoute()); mMediaRouter.getSelectedRoute(), mMediaSession);
mMediaRouter.setMediaSessionCompat(mPlayer.getMediaSession());
mSessionManager.setPlayer(mPlayer); mSessionManager.setPlayer(mPlayer);
mSessionManager.setCallback(new SessionManager.Callback() { mSessionManager.setCallback(new SessionManager.Callback() {
@@ -390,40 +390,42 @@ public class SampleMediaRouterActivity extends ActionBarActivity {
updateUi(); updateUi();
} }
private void registerRCC() { private void createMediaSession() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { // Create the MediaSession
// Create the RCC and register with AudioManager and MediaRouter mMediaSession = new MediaSessionCompat(this, "SampleMediaRouter", mEventReceiver,
mAudioManager.requestAudioFocus(mAfChangeListener, mMediaPendingIntent);
AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
mAudioManager.registerMediaButtonEventReceiver(mEventReceiver); | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mRemoteControlClient = new RemoteControlClient(mMediaPendingIntent); mMediaSession.setCallback(new MediaSessionCompat.Callback() {
mAudioManager.registerRemoteControlClient(mRemoteControlClient); @Override
mMediaRouter.addRemoteControlClient(mRemoteControlClient); public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
SampleMediaButtonReceiver.setActivity(SampleMediaRouterActivity.this); if (mediaButtonEvent != null) {
mRemoteControlClient.setTransportControlFlags( return handleMediaKey(
RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE); (KeyEvent) mediaButtonEvent.getParcelableExtra(Intent.EXTRA_KEY_EVENT));
mRemoteControlClient.setPlaybackState(
RemoteControlClient.PLAYSTATE_PLAYING);
} }
return super.onMediaButtonEvent(mediaButtonEvent);
} }
private void unregisterRCC() { @Override
// Unregister the RCC with AudioManager and MediaRouter public void onPlay() {
if (mRemoteControlClient != null) { mSessionManager.resume();
mRemoteControlClient.setTransportControlFlags(0);
mAudioManager.abandonAudioFocus(mAfChangeListener);
mAudioManager.unregisterMediaButtonEventReceiver(mEventReceiver);
mAudioManager.unregisterRemoteControlClient(mRemoteControlClient);
mMediaRouter.removeRemoteControlClient(mRemoteControlClient);
SampleMediaButtonReceiver.setActivity(null);
mRemoteControlClient = null;
} }
@Override
public void onPause() {
mSessionManager.pause();
}
});
SampleMediaButtonReceiver.setActivity(SampleMediaRouterActivity.this);
} }
public boolean handleMediaKey(KeyEvent event) { public boolean handleMediaKey(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { if (event != null && event.getAction() == KeyEvent.ACTION_DOWN
&& event.getRepeatCount() == 0) {
switch (event.getKeyCode()) { switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
case KeyEvent.KEYCODE_HEADSETHOOK:
{ {
Log.d(TAG, "Received Play/Pause event from RemoteControlClient"); Log.d(TAG, "Received Play/Pause event from RemoteControlClient");
if (mSessionManager.isPaused()) { if (mSessionManager.isPaused()) {
@@ -500,11 +502,9 @@ public class SampleMediaRouterActivity extends ActionBarActivity {
@Override @Override
public void onDestroy() { public void onDestroy() {
// Unregister the remote control client
unregisterRCC();
mSessionManager.stop(); mSessionManager.stop();
mPlayer.release(); mPlayer.release();
mMediaSession.release();
super.onDestroy(); super.onDestroy();
} }
@@ -588,11 +588,6 @@ public class SampleMediaRouterActivity extends ActionBarActivity {
// only enable seek bar when duration is known // only enable seek bar when duration is known
PlaylistItem item = getCheckedPlaylistItem(); PlaylistItem item = getCheckedPlaylistItem();
mSeekBar.setEnabled(item != null && item.getDuration() > 0); mSeekBar.setEnabled(item != null && item.getDuration() > 0);
if (mRemoteControlClient != null) {
mRemoteControlClient.setPlaybackState(mSessionManager.isPaused() ?
RemoteControlClient.PLAYSTATE_PAUSED :
RemoteControlClient.PLAYSTATE_PLAYING);
}
} }
private PlaylistItem getCheckedPlaylistItem() { private PlaylistItem getCheckedPlaylistItem() {