Skip to content
Permalink
Browse files

Switch play and pause buttons according to playback state

  • Loading branch information
ferjm committed Nov 13, 2019
1 parent b5b8c6c commit 07483f1d44cc3ea474e0a47e53253955da3aa4d1
@@ -1790,7 +1790,8 @@ where
match event {
MediaSessionEvent::PlaybackStateChange(ref state) => {
match state {
MediaSessionPlaybackState::Playing => (),
MediaSessionPlaybackState::Playing |
MediaSessionPlaybackState::Paused => (),
_ => return,
};
},
@@ -237,16 +237,17 @@ public void onMediaSessionMetadata(String title, String artist, String album) {

@Override
public void onMediaSessionPlaybackStateChange(int state) {
Log.d("SERVOMEDIA", "PLAYBACK STATE CHANGED " + state);
if (mMediaSession == null) {
mMediaSession = new MediaSession(mServoView, this, getApplicationContext());
}
Log.d("SERVOMEDIA", "PLAYBACK STATE CHANGED " + state);
if (state == MediaSession.PLAYBACK_STATE_NONE) {
mMediaSession.hideMediaSessionControls();
return;
}
if (state == MediaSession.PLAYBACK_STATE_PLAYING) {
mMediaSession.showMediaSessionControls();
if (state == MediaSession.PLAYBACK_STATE_PLAYING ||
state == MediaSession.PLAYBACK_STATE_PAUSED) {
mMediaSession.showMediaSessionControls(state);
return;
}
}
@@ -49,6 +49,7 @@ public int get() {
private static final int ACTION_SEEK_TO = 9;

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_PREV = "org.mozilla.servoview.MainActivity.prev";
private static final String KEY_MEDIA_NEXT = "org.mozilla.servoview.MainActivity.next";
@@ -84,9 +85,14 @@ private void createMediaNotificationChannel() {
}
}

public void showMediaSessionControls() {
public void showMediaSessionControls(int playbackState) {
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);

mMediaSessionActionReceiver = new BroadcastReceiver() {
@@ -95,6 +101,9 @@ public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(KEY_MEDIA_PAUSE)) {
mView.mediaSessionAction(ACTION_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)) {
mView.mediaSessionAction(ACTION_STOP);
Log.d("SERVOMEDIA", "STOP");
@@ -104,11 +113,6 @@ public void onReceive(Context context, Intent intent) {

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);
Notification.Action stopAction =
new Notification.Action(R.drawable.media_session_stop, "Stop",
@@ -119,11 +123,24 @@ public void onReceive(Context context, Intent intent) {
.setSmallIcon(R.drawable.media_session_icon)
.setContentTitle("This is the notification title")
.setVisibility(Notification.VISIBILITY_PUBLIC)
.addAction(pauseAction)
.addAction(stopAction)
.setStyle(new Notification.MediaStyle()
.setShowActionsInCompactView(0 /* pause button */ )
.setShowActionsInCompactView(1 /* stop button */));
.setStyle(new Notification.MediaStyle());

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 =
mContext.getSystemService(NotificationManager.class);
Binary file not shown.

0 comments on commit 07483f1

Please sign in to comment.
You can’t perform that action at this time.