How to get seekable progressbar in your notification that updates your mediaplayer instance in android q programatically?

1.2k views Asked by At

Android Q and above provides functionality to include seek-able progress bar in our notification. I am creating a app that plays audio files from external storage.

MediaPlayer control progressbar

I want to implement the progress bar as shown in the image.

The problem is that i have implemented a progress bar and it is seekable but its doesnot update the mediaplayer when user seeks to a particular position on seekbar .

My implementation of progressbar

The code

Following code sets the metadata

mediaSession.setMetadata(MediaMetadataCompat.fromMediaMetadata(new MediaMetadata.Builder().putString(MediaMetadata.METADATA_KEY_TITLE, title) .putLong(MediaMetadata.METADATA_KEY_DURATION, Long.parseLong(listSongs.get(position).getmDuration())).build()));

Following code checks if mediaplayer is playing or not and sets the PlaybackState

 if(mediaPlayer!=null) {


                if(!mediaPlayer.isPlaying()){
                     playbackState = PlaybackState.STATE_PLAYING;
                }
                else{
                   playbackState =  PlaybackState.STATE_PAUSED;
                }

         mBuilder =  new PlaybackState.Builder()
                .setState(playbackState
                        , musicService.getCurrentPosition()
                        , 1
                )
                .setActions(PlaybackState.ACTION_SEEK_TO| 
                        PlaybackState.ACTION_PAUSE |
                        PlaybackState.ACTION_SKIP_TO_NEXT |
                        PlaybackState.ACTION_SKIP_TO_PREVIOUS |
                        PlaybackState.ACTION_PLAY_PAUSE)
                 ;




        mediaSession.setPlaybackState(PlaybackStateCompat.fromPlaybackState(mBuilder.build()));
        mediaSession.setActive(true);
1

There are 1 answers

0
PLATFORM pTp On

Your code is ok so far .. you just did not set the mediasession callback inorder to retrieve the position (long) from when you track(move) the seekbar. There are other methods you can get from the callback too

Add below code in your service class after the the set metadata

mediasession.setCallback(new Mediasession.Callback(){

//you should be able to get the onSeekTo method from above ..then just pass the long to a integer before updating your mediaPlayer 
}