How do I use a MediaSessionCompat? Can someone give a simple working example?
I've found some, but they use:
MediaSessionCompat _mediaSession = new MediaSessionCompat(context, "tag");
This gives me error:
The constructor MediaSessionCompat(Context, String) is undefined
and wants me to use MediaSessionCompat(Context, String, ComponentName, PendingIntent)
android.support.v4.media.session.MediaSessionCompat. Allows interaction with media controllers, volume keys, media buttons, and transport controls. A MediaSession should be created when an app wants to publish media playback information or handle media keys.
Media browser services enable applications to browse media content provided by an application and ask the application to start playing it. They may also be used to control content that is already playing by way of a MediaSession .
I found a working example here, which I tested both on kitkat and marshmallow.
https://github.com/tutsplus/background-audio-in-android-with-mediasessioncompat/blob/master/app/src/main/java/com/tutsplus/backgroundaudio/BackgroundAudioService.java
But do take note, sometimes, sometimes lock screen control don't appear because of android settings(i.e Settings > Sounds & Notifications > Notification > While Locked > Hide Sensitive Content) See below:
https://community.spotify.com/t5/Android/Android-Lollipop-Lock-Screen-Controls-Not-Available/td-p/982463
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