Skip to content
Permalink
Browse files

Update media session metadata and show content text with artist and a…

…lbum
  • Loading branch information
ferjm committed Nov 15, 2019
1 parent d33c96b commit f65c4008287d6c71b6d3c94183139b16b170b5ca
@@ -233,6 +233,7 @@ public void onMediaSessionMetadata(String title, String artist, String album) {
mMediaSession = new MediaSession(mServoView, this, getApplicationContext());
}
Log.d("onMediaSessionMetadata", title + " " + artist + " " + album);
mMediaSession.updateMetadata(title, artist, album);
}

@Override
@@ -241,13 +242,16 @@ public void onMediaSessionPlaybackStateChange(int state) {
if (mMediaSession == null) {
mMediaSession = new MediaSession(mServoView, this, getApplicationContext());
}

mMediaSession.setPlaybackState(state);

if (state == MediaSession.PLAYBACK_STATE_NONE) {
mMediaSession.hideMediaSessionControls();
return;
}
if (state == MediaSession.PLAYBACK_STATE_PLAYING ||
state == MediaSession.PLAYBACK_STATE_PAUSED) {
mMediaSession.showMediaSessionControls(state);
mMediaSession.showMediaSessionControls();
return;
}
}
@@ -58,9 +58,16 @@ public int get() {
ServoView mView;
MainActivity mActivity;
Context mContext;

NotificationID mNotificationID;
BroadcastReceiver mMediaSessionActionReceiver;

int mPlaybackState = PLAYBACK_STATE_PAUSED;

String mTitle;
String mArtist;
String mAlbum;

public MediaSession(ServoView view, MainActivity activity, Context context) {
mView = view;
mActivity = activity;
@@ -85,13 +92,13 @@ private void createMediaNotificationChannel() {
}
}

public void showMediaSessionControls(int playbackState) {
Log.d("MediaSession", "showMediaSessionControls " + playbackState);
public void showMediaSessionControls() {
Log.d("MediaSession", "showMediaSessionControls " + mPlaybackState);
IntentFilter filter = new IntentFilter();
if (playbackState == PLAYBACK_STATE_PAUSED) {
if (mPlaybackState == PLAYBACK_STATE_PAUSED) {
filter.addAction(KEY_MEDIA_PLAY);
}
if (playbackState == PLAYBACK_STATE_PLAYING) {
if (mPlaybackState == PLAYBACK_STATE_PLAYING) {
filter.addAction(KEY_MEDIA_PAUSE);
}
filter.addAction(KEY_MEDIA_STOP);
@@ -129,20 +136,36 @@ public void onReceive(Context context, Intent intent) {
Notification.Builder builder = new Notification.Builder(mContext, this.MEDIA_CHANNEL_ID);
builder
.setSmallIcon(R.drawable.media_session_icon)
.setContentTitle("This is the notification title")
.setContentTitle(mTitle)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.addAction(stopAction)
.setStyle(new Notification.MediaStyle());

if (playbackState == PLAYBACK_STATE_PAUSED) {
String contentText = new String();
if (mArtist != null && !mArtist.isEmpty()) {
contentText = mArtist;
}
if (mAlbum != null && !mAlbum.isEmpty()) {
if (!contentText.isEmpty()) {
contentText += " - " + mAlbum;
} else {
contentText = mAlbum;
}
}

if (!contentText.isEmpty()) {
builder.setContentText(contentText);
}

if (mPlaybackState == 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) {
if (mPlaybackState == PLAYBACK_STATE_PLAYING) {
Intent pauseIntent = new Intent(KEY_MEDIA_PAUSE);
Notification.Action pauseAction =
new Notification.Action(R.drawable.media_session_pause, "Pause",
@@ -163,4 +186,18 @@ public void hideMediaSessionControls() {
mContext.unregisterReceiver(mMediaSessionActionReceiver);
mMediaSessionActionReceiver = null;
}

public void setPlaybackState(int state) {
mPlaybackState = state;
}

public void updateMetadata(String title, String artist, String album) {
mTitle = title;
mArtist = artist;
mAlbum = album;

if (mMediaSessionActionReceiver != null) {
showMediaSessionControls();
}
}
}

0 comments on commit f65c400

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