Media Session Compat not showing Lockscreen controls on Pre-Lollipop

前端 未结 3 1389
一个人的身影
一个人的身影 2021-01-30 07:14

I\'m using MediaSessionCompat from AppCompat Support Library Revision 22. And on Lollipop I\'m getting notification & also the background of lockscreen is the a

3条回答
  •  星月不相逢
    2021-01-30 07:41

    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 {
    @Override
    public 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;
                }
            }
        }
    }
    

提交回复
热议问题