How to use MediaSessionCompat?

How do I use a MediaSessionCompat? Can someone give a simple working example?

I’ve found some, but they use:

  • How to integrate OpenCV into Qt Creator Android project
  • Gradle sync failed: Unable to find method
  • Windows 8.1 Device Manager now showing ACER Device rather than Android Device for Google Nexus 7
  • Make Android Activity looks like dialog
  • Create TextView from template XML file
  • Java - Convert String to valid URI object
  • 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)

  • ActivityNotFoundException when different package's targetClass in PreferenceScreen
  • Create Android library AAR including javadoc and sources
  • How to write style to error text of EditText in android?
  • Text-transform:uppercase equivalent in Android?
  • Android dialer application
  • How can I create a keystore?
  • 3 Solutions collect form web for “How to use MediaSessionCompat?”

    SampleMediaRouterActivity.java in Support7Demos seems to be a good place to start.

    https://android.googlesource.com/platform/development/+/master/samples/Support7Demos/src/com/example/android/supportv7/media/

    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

    MediaSessionCompat is in android.support.v4 and that is the Constructor it uses.. it is a backward compatibility for MediaSession introduced in api 21, and this is its Contrustor;. However if you want to use the Constructor you are referring to you need to compile your project with api 21+;

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.