Fix play action

This commit is contained in:
Fernando Jiménez Moreno 2019-11-13 16:50:02 +01:00
parent 07483f1d44
commit 7b5b46f560
2 changed files with 27 additions and 18 deletions

View file

@ -232,12 +232,12 @@ public class MainActivity extends Activity implements Servo.Client {
if (mMediaSession == null) { if (mMediaSession == null) {
mMediaSession = new MediaSession(mServoView, this, getApplicationContext()); mMediaSession = new MediaSession(mServoView, this, getApplicationContext());
} }
Log.d("SERVOMEDIA", "METADATA"); Log.d("onMediaSessionMetadata", title + " " + artist + " " + album);
} }
@Override @Override
public void onMediaSessionPlaybackStateChange(int state) { public void onMediaSessionPlaybackStateChange(int state) {
Log.d("SERVOMEDIA", "PLAYBACK STATE CHANGED " + state); Log.d("onMediaSessionPlaybackStateChange", String.valueOf(state));
if (mMediaSession == null) { if (mMediaSession == null) {
mMediaSession = new MediaSession(mServoView, this, getApplicationContext()); mMediaSession = new MediaSession(mServoView, this, getApplicationContext());
} }

View file

@ -86,6 +86,7 @@ public class MediaSession {
} }
public void showMediaSessionControls(int playbackState) { public void showMediaSessionControls(int playbackState) {
Log.d("MediaSession", "showMediaSessionControls " + playbackState);
IntentFilter filter = new IntentFilter(); IntentFilter filter = new IntentFilter();
if (playbackState == PLAYBACK_STATE_PAUSED) { if (playbackState == PLAYBACK_STATE_PAUSED) {
filter.addAction(KEY_MEDIA_PLAY); filter.addAction(KEY_MEDIA_PLAY);
@ -95,21 +96,28 @@ public class MediaSession {
} }
filter.addAction(KEY_MEDIA_STOP); filter.addAction(KEY_MEDIA_STOP);
int id;
if (mMediaSessionActionReceiver == null) {
id = mNotificationID.getNext();
mMediaSessionActionReceiver = new BroadcastReceiver() { mMediaSessionActionReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
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("MediaSession", "PAUSE action");
} else if (intent.getAction().equals(KEY_MEDIA_PLAY)) { } else if (intent.getAction().equals(KEY_MEDIA_PLAY)) {
mView.mediaSessionAction(ACTION_PLAY); mView.mediaSessionAction(ACTION_PLAY);
Log.d("SERVOMEDIA", "PLAY"); Log.d("MediaSession", "PLAY action");
} 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("MediaSession", "STOP action");
} }
} }
}; };
} else {
id = mNotificationID.get();
}
mContext.registerReceiver(mMediaSessionActionReceiver, filter); mContext.registerReceiver(mMediaSessionActionReceiver, filter);
@ -144,14 +152,15 @@ public class MediaSession {
NotificationManager notificationManager = NotificationManager notificationManager =
mContext.getSystemService(NotificationManager.class); mContext.getSystemService(NotificationManager.class);
notificationManager.notify(mNotificationID.getNext(), builder.build()); notificationManager.notify(id, builder.build());
} }
public void hideMediaSessionControls() { public void hideMediaSessionControls() {
Log.d("SERVOMEDIA", "hideMediaSessionControls"); Log.d("MediaSession", "hideMediaSessionControls");
NotificationManager notificationManager = NotificationManager notificationManager =
mContext.getSystemService(NotificationManager.class); mContext.getSystemService(NotificationManager.class);
notificationManager.cancel(mNotificationID.get()); notificationManager.cancel(mNotificationID.get());
mContext.unregisterReceiver(mMediaSessionActionReceiver); mContext.unregisterReceiver(mMediaSessionActionReceiver);
mMediaSessionActionReceiver = null;
} }
} }