Search code examples
androidandroid-mediaplayerrunnableandroid-seekbarandroid-mediasession

android mediaSession getPosition() always returns 0


I am trying to get the current position so that I can progress the seek-bar in my media player app. However, every time I call:

getSupportMediaController().getPlaybackState().getPosition();

It returns 0 if the media is playing and if it is paused it returns -1. I searched a lot for a solution with no success and I tried everything.

By the way I put the code above inside of a runner so that it gets executed every 1 second.


Solution

  • PlaybackStateCompat is just a wrapper object to get the playback information through your Mediaplay or Exoplayer (whatever you're using)

    You need to update the playback state object in your MediaSessionCompat using the setState method in the PlaybackState.Builder

    setState
    

    https://developer.android.com/reference/android/support/v4/media/session/PlaybackStateCompat.Builder.html#setState

    setPlaybackState
    

    https://developer.android.com/reference/android/support/v4/media/session/MediaSessionCompat.html#setPlaybackState