Media Session Compat not showing Lockscreen controls on Pre-Lollipop
While not strictly required for MediaSession
, RemoteControlClient
used on API14-19 devices, does require audio focus and it is 100% strongly recommended for all media playback.
Adding lines such as:
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);if (result != AudioManager.AUDIOFOCUS_GAIN) { return; //Failed to gain audio focus}
Before playing any media should gain audio focus and show controls.
Finally I figured a solution for this. Thanks to @ianhanniballake & @user1549672
- Add Audio Focus as suggested by @ianhanniballake
- Add Music Intent
BroadcastReceiver
can be found if searched on Google & also on Official Android Docs - Write the
setupMediaSession()
given in my question above - VERY IMPORTANT Mention the
Flags
properly - Write
MediaSessionCallbacks
also available above - VERY IMPORTANT Update the
MediaSession
onMediaPlayer#onPause()
,MediaPlayer#onStart()
& finally when new song is played (also includes next & previous played) mentioned by @user1549672 - Release the
MediaSession
object inonDestory()
Well that's all, most of the material (code) is available above. This question took couple of months to solve, finally it's done.
Finally I got an answer to your's and my problem .Issue is you need to specify actions (mMediaSessionCompat.setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE) even while updating mediasession. So your code should now look like
private void updateMediaSessionMetaData() { int playState = mPlaying ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED; mMediaSessionCompat.setMetadata(new MediaMetadataCompat.Builder() .putString(MediaMetadata.METADATA_KEY_ARTIST, getArtist()) .putString(MediaMetadata.METADATA_KEY_ALBUM, getAlbum()) .putString(MediaMetadata.METADATA_KEY_TITLE, getSongTitle()) .putLong(MediaMetadata.METADATA_KEY_DURATION, duration()) .putLong(MediaMetadata.METADATA_KEY_TRACK_NUMBER, mSongPosn) .putLong(MediaMetadata.METADATA_KEY_NUM_TRACKS, songs.size()) .putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, albumArt) .build());mMediaSessionCompat.setPlaybackState(new PlaybackStateCompat.Builder() .setState(playState, position(), 1.0f) .setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE| PlaybackStateCompat.ACTION_SKIP_TO_NEXT|PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS).build());
UPDATE : Added code for MediaCallback & Receiver
private final class MediaSessionCallback extends MediaSessionCompat.Callback { @Override public void onPlay() { pausePlayer(); } @Override public void onPause() { pausePlayer(); } public void onSeekTo(long pos) { seek(pos); } @Override public void onSkipToNext() { playNext(); } @Override public void onSkipToPrevious() { playPrev(); }}
Receiver :
public class MusicIntentReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) { if (intent.getAction().equals( android.media.AudioManager.ACTION_AUDIO_BECOMING_NOISY)) { Intent intent1 = new Intent(MusicService.ACTION_PAUSE); intent1.setClass(context, com.xyz.service.MusicService.class); // send an intent to our MusicService to telling it to pause the // audio context.startService(intent1); } else if (intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON)) { KeyEvent keyEvent = (KeyEvent) intent.getExtras().get( Intent.EXTRA_KEY_EVENT); if (keyEvent.getAction() != KeyEvent.ACTION_DOWN) return; switch (keyEvent.getKeyCode()) { case KeyEvent.KEYCODE_HEADSETHOOK: case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: Intent intentToggle = new Intent( MusicService.ACTION_TOGGLE_PLAYBACK); intentToggle.setClass(context, com.xyz.service.MusicService.class); context.startService(intentToggle); break; case KeyEvent.KEYCODE_MEDIA_PLAY: Intent intentPlay = new Intent(MusicService.ACTION_PLAY); intentPlay.setClass(context, com.xyz.service.MusicService.class); context.startService(intentPlay); break; case KeyEvent.KEYCODE_MEDIA_PAUSE: Intent intentPause = new Intent(MusicService.ACTION_PAUSE); intentPause.setClass(context, com.xyz.service.MusicService.class); context.startService(intentPause); break; case KeyEvent.KEYCODE_MEDIA_NEXT: Intent intentNext = new Intent(MusicService.ACTION_NEXT); intentNext.setClass(context, com.xyz.service.MusicService.class); context.startService(intentNext); break; case KeyEvent.KEYCODE_MEDIA_PREVIOUS: Intent intentPrev = new Intent(MusicService.ACTION_PREV); intentPrev.setClass(context, com.xyz.service.MusicService.class); context.startService(intentPrev); break; default: break; } } }}