Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use MediaSessionCompat?

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)

like image 581
Gintas_ Avatar asked May 27 '15 15:05

Gintas_


People also ask

What is MediaSessionCompat in android?

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.

What is media browser service?

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 .


1 Answers

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

like image 111
ChinLoong Avatar answered Nov 09 '22 13:11

ChinLoong