I want to use MediaSession with exoplayer and by searching I found that Exoplayer already have MediaSession extension library(https://github.com/google/ExoPlayer/tree/release-v2/extensions/mediasession), but I am unable to find any good documentation on how to implement that.
I have already read the documentation provided by Google developer on this but it is not clear and hard to understand for me, the documentation link is https://medium.com/google-exoplayer/the-mediasession-extension-for-exoplayer-82b9619deb2d
Can anyone please help me how can I implement MediaSession extension with Exoplayer.
Edited:
Finally I was able to implement this by trying hard using the above link (https://medium.com/google-exoplayer/the-mediasession-extension-for-exoplayer-82b9619deb2d).
Details are given in the answer section below.
To map the playback state of the player to the media session you can do as follows (assuming video playback in an activity):
// onCreate()
mediaSession = new MediaSessionCompat(this, getPackageName());
mediaSessionConnector = new MediaSessionConnector(mediaSession)
// onStart() or onResume() according to API level
initializePlayer();
mediaSessionConnector.setPlayer(player, null, null);
mediaSession.setActive(true);
// onPause() or onStop() according to API level
mediaSessionConnector.setPlayer(null, null, null);
mediaSession.setActive(false);
releasePlayer();
With this media actions like ACTION_PLAY, ACTION_PAUSE and the like are already supported.
You can find some more context in another SOF post.
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