MediaButtonReceiver
open class MediaButtonReceiver : BroadcastReceiver
kotlin.Any | ||
↳ | android.content.BroadcastReceiver | |
↳ | androidx.media.session.MediaButtonReceiver |
A media button receiver receives and helps translate hardware media playback buttons, such as those found on wired and wireless headsets, into the appropriate callbacks in your app.
You can add this MediaButtonReceiver to your app by adding it directly to your AndroidManifest.xml:<receiver android:name="androidx.media.session.MediaButtonReceiver" > <intent-filter> <action android:name="android.intent.action.MEDIA_BUTTON" /> </intent-filter> </receiver>This class assumes you have a
Service
in your app that controls media playback via a MediaSessionCompat
. Once a key event is received by MediaButtonReceiver, this class tries to find a Service
that can handle Intent#ACTION_MEDIA_BUTTON
, and a MediaBrowserServiceCompat
in turn. If an appropriate service is found, this class forwards the key event to the service. If neither is available or more than one valid service/media browser service is found, an IllegalStateException
will be thrown. Thus, your app should have one of the following services to get a key event properly.
Service Handling ACTION_MEDIA_BUTTON
A service can receive a key event by including an intent filter that handlesIntent#ACTION_MEDIA_BUTTON
:
<service android:name="com.example.android.MediaPlaybackService" > <intent-filter> <action android:name="android.intent.action.MEDIA_BUTTON" /> </intent-filter> </service>Events can then be handled in
Service#onStartCommand(Intent, int, int)
by calling MediaButtonReceiver#handleIntent(MediaSessionCompat, Intent)
, passing in your current MediaSessionCompat
:
private MediaSessionCompat mMediaSessionCompat = ...; public int onStartCommand(Intent intent, int flags, int startId) { MediaButtonReceiver.handleIntent(mMediaSessionCompat, intent); return super.onStartCommand(intent, flags, startId); }This ensures that the correct callbacks to
MediaSessionCompat.Callback
will be triggered based on the incoming KeyEvent
.
Note: Once the service is started, it must start to run in the foreground.
MediaBrowserService
If you already have aMediaBrowserServiceCompat
in your app, MediaButtonReceiver will deliver the received key events to the MediaBrowserServiceCompat
by default. You can handle them in your MediaSessionCompat.Callback
.
Summary
Public constructors | |
---|---|
<init>() A media button receiver receives and helps translate hardware media playback buttons, such as those found on wired and wireless headsets, into the appropriate callbacks in your app. |
Public methods | |
---|---|
open static PendingIntent! |
buildMediaButtonPendingIntent(context: Context!, action: Long) Creates a broadcast pending intent that will send a media button event. |
open static PendingIntent! |
buildMediaButtonPendingIntent(context: Context!, mbrComponent: ComponentName!, action: Long) Creates a broadcast pending intent that will send a media button event. |
open static KeyEvent! |
handleIntent(mediaSessionCompat: MediaSessionCompat!, intent: Intent!) Extracts any available |
open Unit |
Public constructors
<init>
MediaButtonReceiver()
A media button receiver receives and helps translate hardware media playback buttons, such as those found on wired and wireless headsets, into the appropriate callbacks in your app.
You can add this MediaButtonReceiver to your app by adding it directly to your AndroidManifest.xml:<receiver android:name="androidx.media.session.MediaButtonReceiver" > <intent-filter> <action android:name="android.intent.action.MEDIA_BUTTON" /> </intent-filter> </receiver>This class assumes you have a
Service
in your app that controls media playback via a MediaSessionCompat
. Once a key event is received by MediaButtonReceiver, this class tries to find a Service
that can handle Intent#ACTION_MEDIA_BUTTON
, and a MediaBrowserServiceCompat
in turn. If an appropriate service is found, this class forwards the key event to the service. If neither is available or more than one valid service/media browser service is found, an IllegalStateException
will be thrown. Thus, your app should have one of the following services to get a key event properly.
Service Handling ACTION_MEDIA_BUTTON
A service can receive a key event by including an intent filter that handlesIntent#ACTION_MEDIA_BUTTON
:
<service android:name="com.example.android.MediaPlaybackService" > <intent-filter> <action android:name="android.intent.action.MEDIA_BUTTON" /> </intent-filter> </service>Events can then be handled in
Service#onStartCommand(Intent, int, int)
by calling MediaButtonReceiver#handleIntent(MediaSessionCompat, Intent)
, passing in your current MediaSessionCompat
:
private MediaSessionCompat mMediaSessionCompat = ...; public int onStartCommand(Intent intent, int flags, int startId) { MediaButtonReceiver.handleIntent(mMediaSessionCompat, intent); return super.onStartCommand(intent, flags, startId); }This ensures that the correct callbacks to
MediaSessionCompat.Callback
will be triggered based on the incoming KeyEvent
.
Note: Once the service is started, it must start to run in the foreground.
MediaBrowserService
If you already have aMediaBrowserServiceCompat
in your app, MediaButtonReceiver will deliver the received key events to the MediaBrowserServiceCompat
by default. You can handle them in your MediaSessionCompat.Callback
.Public methods
buildMediaButtonPendingIntent
open static fun buildMediaButtonPendingIntent(
context: Context!,
action: Long
): PendingIntent!
Creates a broadcast pending intent that will send a media button event. The action
will be translated to the appropriate KeyEvent
, and it will be sent to the registered media button receiver in the given context. The action
should be one of the following: