androidmediacontrollerandroid-mediasession

MediaControllerCompat get state doesn't return the current position


In my code

MediaControllerCompat.setMediaController(MainActivity.this, mediaController);
mediaController.registerCallback(mCallback);
PlaybackStateCompat state = mediaController.getPlaybackState();

The getPosition() in state is always the one I set in MediaSessionCompat.Callback onPlay and onPause methods.

I want to know how to set the state of MediaSessionCompat object so that mediacontroller getPlaybackState() returns with the latest position?


Solution

  • You will get a valid position only if you update the position parameter and update the PlaybackState object.

    You can create a looper the wakes up every second and update the PlaybackState with the setPlaybackState method. Create a PlaybackStateCompat object with the builder and use the setState method to set the position.