Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android 10 seek bar on notification

does anyone know about the seekbar function on notifications in android 10?

I can't seem to get the position indicator to show the current position. the bar is just blank but I can seek the media clicking the bar in the notification.

I have the normal notification builder code and I have added this to make the seekbar clickable in the "MediaPlayer.Event.Playing" event of libvlc

MediaMetadataCompat.Builder metadataBuilder = new MediaMetadataCompat.Builder();


metadataBuilder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, mMediaPlayer.getLength());

PlaybackStateCompat.Builder mStateBuilder = new PlaybackStateCompat.Builder()
        .setState(PlaybackStateCompat.STATE_PLAYING,  1, 1.0f)
        .setBufferedPosition(mMediaPlayer.getLength())
        .setActions(
                PlaybackStateCompat.ACTION_PLAY |
                        PlaybackStateCompat.ACTION_PAUSE |
                        PlaybackStateCompat.ACTION_SKIP_TO_NEXT |
                        PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
                        PlaybackStateCompat.ACTION_SEEK_TO |
                        PlaybackStateCompat.ACTION_PLAY_PAUSE);


mediaSession.setMetadata(metadataBuilder.build());
mediaSession.setPlaybackState(mStateBuilder.build());

the media playing is about half way but there is no indicator of its position.

notification

like image 313
somecant Avatar asked Dec 29 '19 05:12

somecant


1 Answers

After searching for a long time, eventually I came up with the solution. Please take the below steps to have the SeekBar displayed in your notification in Android 10 or higher.

  • Step 1: Put the duration of your file in milliseconds as a Long value with the key MediaMetadataCompat.METADATA_KEY_DURATION in your MediaSession.setMetadata
mediaSession.setMetadata(MediaMetadataCompat.Builder()
    .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, resource)
    .putString(
        MediaMetadataCompat.METADATA_KEY_ARTIST,
        activeBookFile.title
    )
    .putString(
        MediaMetadataCompat.METADATA_KEY_ALBUM,
        AudioPlayerService.activeBook.title
    )
    .putString(
        MediaMetadataCompat.METADATA_KEY_TITLE,
        AudioPlayerService.activeBook.title
    )
    .putLong(
        MediaMetadataCompat.METADATA_KEY_DURATION,
        activeBookFile.duration
    )
    .build()
)
  • Step 2: Add ACTION_SEEK_TO to all your PlaybackStateCompat.setAction() methods
  • Step 3: Provide a valid currentPosition in millseconds and the current state of your playing item then pass them to PlaybackStateCompat.setState() method.
  • Step 4: Set the playBackState object to your mediaSession using setPlaybackState()
fun updateMediaPlaybackState(currentPos: Long) {
    mediaSession.setPlaybackState(
        PlaybackStateCompat.Builder()
            .setActions(
                PlaybackStateCompat.ACTION_PLAY
                        or PlaybackStateCompat.ACTION_PLAY_PAUSE
                        or PlaybackStateCompat.ACTION_PAUSE
                        or PlaybackStateCompat.ACTION_SKIP_TO_NEXT
                        or PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS
                        or PlaybackStateCompat.ACTION_SEEK_TO
            )
            .setState(
                getPlaybackState(),
                currentPos,
                1f,
                SystemClock.elapsedRealtime()
            )
            .build()
    )
}
@PlaybackStateCompat.State
private fun getPlaybackState(): Int {
        return when (currentState) {
            is PlayerStoppedState -> PlaybackStateCompat.STATE_STOPPED
            is PlayerPausedState -> PlaybackStateCompat.STATE_PAUSED
            is PlayerResumingState -> PlaybackStateCompat.STATE_PLAYING
            is PlayerLoadingState, is PlayerBufferingState -> PlaybackStateCompat.STATE_BUFFERING
            is PlayerErrorState -> PlaybackStateCompat.STATE_ERROR
            else -> PlaybackStateCompat.STATE_NONE
        }
    }
  • Step 5: To support user touch on the SeekBar override onSeekTo(pos: Long) method in MediaSessionCompat.Callback()
class PlayerMediaSessionCallback(mediaPlayer: MediaPlayer?) : MediaSessionCompat.Callback() {

// other callback methods

    override fun onSeekTo(pos: Long) {
        mediaPlayer?.seekTo(pos)
    }
}
  • Note: Remember to call updateMediaPlaybackState() and showNotification() whenever a state change happened for your player(paly, pause, skipNext, skipPrevious, seek, etc.)
like image 70
Seyyed Mohsen Mousavi Avatar answered Sep 27 '22 18:09

Seyyed Mohsen Mousavi