Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use MediaSession extension for ExoPlayer v2.5+

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.

like image 438
Ravi R Priyadarshi Avatar asked Dec 19 '22 03:12

Ravi R Priyadarshi


1 Answers

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.

like image 161
marcbaechinger Avatar answered Jan 12 '23 15:01

marcbaechinger