As described on this documentation page: https://developer.android.com/guide/topics/media/legacy/media-buttons, the route a media button event takes depends on the Android version. On API versions 21+, it is the media session handling the event, with some differences throughout later API versions in finding the correct media session instance.
There's also the MediaButtonReceiver (https://developer.android.com/reference/androidx/media/session/MediaButtonReceiver), a broadcast receiver that also helps in handling media button events. It is mentioned on several occasions that it is a helper for API versions prior to 21. Most examples still include it in the manifest:
<receiver android:name="androidx.media.session.MediaButtonReceiver"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
I use a MediaBrowserServiceCompat to enable background audio playback, and only support Android API versions 21+. But I want to be able to disable the MediaBrowserServiceCompat if no support for background playback is requested. As the androidx.media.session.MediaButtonReceiver looks for a service that can handle the media button event, it will annoyingly throw an exception if it can't find the disabled MediaBrowserServiceCompat.
So my question is: if I only support Android API versions 21+, has the MediaButtonReceiver a purpose at all, or should the media session take care of all media button event routing?
The purpose of the
MediaButtonReceiver
in the context of your application depends on the Android API versions you are targeting. Since you mentioned that you only support Android API versions 21 & above, theMediaButtonReceiver
may not have a purpose for your specific use case.In API versions 21 & above, media button events are typically handled by the media session. The media session is responsible for managing media playback & handling media button events. The media session can be set up & controlled using the
MediaSessionCompat
class from the Android Support Library.The
MediaButtonReceiver
is primarily used as a helper for handling media button events on API versions prior to 21. It is designed to receive theandroid.intent.action.MEDIA_BUTTON
broadcast and deliver it to the appropriate media session or media session callback. By including theMediaButtonReceiver
in your manifest file, you ensure that media button events are properly routed to your app on older Android versions.Since you only support API versions 21 & above, the media session should be sufficient for handling media button events in your application. You can register a
MediaSessionCompat.Callback
to receive media button events & handle them accordingly.Here's an example of how you can set up a media session & handle media button events:
By implementing the
onMediaButtonEvent
method in theMediaSessionCompat.Callback
, you can handle different media button events such as play/pause, next, & previous. This approach allows you to handle media button events directly within the media session without the need for theMediaButtonReceiver
.