Switch play and pause buttons according to playback state

This commit is contained in:
Fernando Jiménez Moreno 2019-11-13 12:09:33 +01:00
parent b5b8c6c2a8
commit 07483f1d44
4 changed files with 34 additions and 15 deletions

View file

@ -1790,7 +1790,8 @@ where
match event { match event {
MediaSessionEvent::PlaybackStateChange(ref state) => { MediaSessionEvent::PlaybackStateChange(ref state) => {
match state { match state {
MediaSessionPlaybackState::Playing => (), MediaSessionPlaybackState::Playing |
MediaSessionPlaybackState::Paused => (),
_ => return, _ => return,
}; };
}, },

View file

@ -237,16 +237,17 @@ public class MainActivity extends Activity implements Servo.Client {
@Override @Override
public void onMediaSessionPlaybackStateChange(int state) { public void onMediaSessionPlaybackStateChange(int state) {
Log.d("SERVOMEDIA", "PLAYBACK STATE CHANGED " + state);
if (mMediaSession == null) { if (mMediaSession == null) {
mMediaSession = new MediaSession(mServoView, this, getApplicationContext()); mMediaSession = new MediaSession(mServoView, this, getApplicationContext());
} }
Log.d("SERVOMEDIA", "PLAYBACK STATE CHANGED " + state);
if (state == MediaSession.PLAYBACK_STATE_NONE) { if (state == MediaSession.PLAYBACK_STATE_NONE) {
mMediaSession.hideMediaSessionControls(); mMediaSession.hideMediaSessionControls();
return; return;
} }
if (state == MediaSession.PLAYBACK_STATE_PLAYING) { if (state == MediaSession.PLAYBACK_STATE_PLAYING ||
mMediaSession.showMediaSessionControls(); state == MediaSession.PLAYBACK_STATE_PAUSED) {
mMediaSession.showMediaSessionControls(state);
return; return;
} }
} }

View file

@ -49,6 +49,7 @@ public class MediaSession {
private static final int ACTION_SEEK_TO = 9; private static final int ACTION_SEEK_TO = 9;
private static final String MEDIA_CHANNEL_ID = "MediaNotificationChannel"; private static final String MEDIA_CHANNEL_ID = "MediaNotificationChannel";
private static final String KEY_MEDIA_PLAY = "org.mozilla.servoview.MainActivity.play";
private static final String KEY_MEDIA_PAUSE = "org.mozilla.servoview.MainActivity.pause"; private static final String KEY_MEDIA_PAUSE = "org.mozilla.servoview.MainActivity.pause";
private static final String KEY_MEDIA_PREV = "org.mozilla.servoview.MainActivity.prev"; private static final String KEY_MEDIA_PREV = "org.mozilla.servoview.MainActivity.prev";
private static final String KEY_MEDIA_NEXT = "org.mozilla.servoview.MainActivity.next"; private static final String KEY_MEDIA_NEXT = "org.mozilla.servoview.MainActivity.next";
@ -84,9 +85,14 @@ public class MediaSession {
} }
} }
public void showMediaSessionControls() { public void showMediaSessionControls(int playbackState) {
IntentFilter filter = new IntentFilter(); IntentFilter filter = new IntentFilter();
filter.addAction(KEY_MEDIA_PAUSE); if (playbackState == PLAYBACK_STATE_PAUSED) {
filter.addAction(KEY_MEDIA_PLAY);
}
if (playbackState == PLAYBACK_STATE_PLAYING) {
filter.addAction(KEY_MEDIA_PAUSE);
}
filter.addAction(KEY_MEDIA_STOP); filter.addAction(KEY_MEDIA_STOP);
mMediaSessionActionReceiver = new BroadcastReceiver() { mMediaSessionActionReceiver = new BroadcastReceiver() {
@ -95,6 +101,9 @@ public class MediaSession {
if (intent.getAction().equals(KEY_MEDIA_PAUSE)) { if (intent.getAction().equals(KEY_MEDIA_PAUSE)) {
mView.mediaSessionAction(ACTION_PAUSE); mView.mediaSessionAction(ACTION_PAUSE);
Log.d("SERVOMEDIA", "PAUSE"); Log.d("SERVOMEDIA", "PAUSE");
} else if (intent.getAction().equals(KEY_MEDIA_PLAY)) {
mView.mediaSessionAction(ACTION_PLAY);
Log.d("SERVOMEDIA", "PLAY");
} else if (intent.getAction().equals(KEY_MEDIA_STOP)) { } else if (intent.getAction().equals(KEY_MEDIA_STOP)) {
mView.mediaSessionAction(ACTION_STOP); mView.mediaSessionAction(ACTION_STOP);
Log.d("SERVOMEDIA", "STOP"); Log.d("SERVOMEDIA", "STOP");
@ -104,11 +113,6 @@ public class MediaSession {
mContext.registerReceiver(mMediaSessionActionReceiver, filter); mContext.registerReceiver(mMediaSessionActionReceiver, filter);
Intent pauseIntent = new Intent(KEY_MEDIA_PAUSE);
Notification.Action pauseAction =
new Notification.Action(R.drawable.media_session_pause, "Pause",
PendingIntent.getBroadcast(mContext, 0, pauseIntent, 0));
Intent stopIntent = new Intent(KEY_MEDIA_STOP); Intent stopIntent = new Intent(KEY_MEDIA_STOP);
Notification.Action stopAction = Notification.Action stopAction =
new Notification.Action(R.drawable.media_session_stop, "Stop", new Notification.Action(R.drawable.media_session_stop, "Stop",
@ -119,11 +123,24 @@ public class MediaSession {
.setSmallIcon(R.drawable.media_session_icon) .setSmallIcon(R.drawable.media_session_icon)
.setContentTitle("This is the notification title") .setContentTitle("This is the notification title")
.setVisibility(Notification.VISIBILITY_PUBLIC) .setVisibility(Notification.VISIBILITY_PUBLIC)
.addAction(pauseAction)
.addAction(stopAction) .addAction(stopAction)
.setStyle(new Notification.MediaStyle() .setStyle(new Notification.MediaStyle());
.setShowActionsInCompactView(0 /* pause button */ )
.setShowActionsInCompactView(1 /* stop button */)); if (playbackState == PLAYBACK_STATE_PAUSED) {
Intent playIntent = new Intent(KEY_MEDIA_PLAY);
Notification.Action playAction =
new Notification.Action(R.drawable.media_session_play, "Play",
PendingIntent.getBroadcast(mContext, 0, playIntent, 0));
builder.addAction(playAction);
}
if (playbackState == PLAYBACK_STATE_PLAYING) {
Intent pauseIntent = new Intent(KEY_MEDIA_PAUSE);
Notification.Action pauseAction =
new Notification.Action(R.drawable.media_session_pause, "Pause",
PendingIntent.getBroadcast(mContext, 0, pauseIntent, 0));
builder.addAction(pauseAction);
}
NotificationManager notificationManager = NotificationManager notificationManager =
mContext.getSystemService(NotificationManager.class); mContext.getSystemService(NotificationManager.class);

Binary file not shown.

After

Width:  |  Height:  |  Size: 227 B