Responding to hardware media controls in your app
Having your app respond to media controls, such as Play, Pause, Skip, and so on, is a nice touch your users will appreciate.
Android makes this possible through the media library. As with the Playing sound effects with SoundPool recipe earlier, the Lollipop release changed how this is done. Unlike the SoundPool
example, this recipe is able to take advantage of another approach—the compatibility library.
This recipe will show you how to set up MediaSession
to respond to the hardware buttons, which will work on Lollipop and later, as well as previous Lollilop
versions using the MediaSessionCompat
library. (The Compatibility Library will take care of checking the OS version and using the correct API calls automatically.)
Getting ready
Create a new project in Android Studio and call it: HardwareMediaControls
. Use the default Phone & Tablet options and select Empty Activity when prompted for the Activity Type.
How to do it...
We'll just be using...