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.
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.
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()
)
ACTION_SEEK_TO
to all your PlaybackStateCompat.setAction()
methodsPlaybackStateCompat.setState()
method.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
}
}
onSeekTo(pos: Long)
method in MediaSessionCompat.Callback()
class PlayerMediaSessionCallback(mediaPlayer: MediaPlayer?) : MediaSessionCompat.Callback() {
// other callback methods
override fun onSeekTo(pos: Long) {
mediaPlayer?.seekTo(pos)
}
}
updateMediaPlaybackState()
and showNotification()
whenever a state change happened for your player(paly, pause, skipNext, skipPrevious, seek, etc.)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With