Responding to hardware media controls in your app
Having your app respond tomedia controls (like on headphones), 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 theSoundPool
example, this recipe is able to take advantage of another approach, the compatibility library.
This recipe will show you how to set upMediaSession
to respond to the hardware buttons, which will work on Lollipop and later, as well as previous Lollipop versions using theMediaSessionCompat
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 itHardwareMediaControls
. Use the default Phone & Tablet
options and select Empty Activity
on the Add an Activity
to Mobile
dialog.
How to do it...
We...