Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for an example of the new Android API setMediaButtonReceiver

Currently I am using

  mAudioManager.registerMediaButtonEventReceiver(mRemoteControlResponder);

but this is now deprecated in 5.0 and replaced by setMediaButtonReceiver. There are 5 links in Google all pointing to developer.android.com.

Has anyone used this yet? If so can you provide an example?

like image 616
John Smith Avatar asked Nov 09 '14 03:11

John Smith


People also ask

What is mediabuttonreceiver in Android?

Prior to Android 5.0 (API level 21), Android handles media button events by broadcasting an intent with the ACTION_MEDIA_BUTTON action. Your app must register a BroadcastReceiver to intercept these intents. The MediaButtonReceiver class was designed specifically for this purpose.

How do I get the media button event on Android?

On Android 5.0 (API level 21) and higher, Android automatically dispatches media button events to your active media session by calling onMediaButtonEvent (). By default this callback translates the KeyEvent into the appropriate media session Callback method that matches the key code.

How do I restart a mediabuttonreceiver session?

See the MediaButtonReceiver documentation for more information. If Android can identify the last active media session, it tries to restart the session by sending an ACTION_MEDIA_BUTTON Intent to a manifest-registered component (such as a service or BroadcastReceiver ).

How do I handle media buttons in an active media session?

Handling media buttons in an active media session. On Android 5.0 (API level 21) and higher, Android automatically dispatches media button events to your active media session by calling onMediaButtonEvent(). By default this callback translates the KeyEvent into the appropriate media session Callback method that matches the key code.


1 Answers

Check this page: http://grepcode.com/file/repo1.maven.org/maven2/org.robolectric/android-all/5.0.0_r2-robolectric-0/android/media/session/MediaSession.java It is a rather large example of the complete flow.Here one of the most relevant parts

Set a pending intent for your media button receiver to allow restarting playback after the session has been stopped. If your app is started in this way an android.content.Intent.ACTION_MEDIA_BUTTON intent will be sent via the pending intent.

Parameters: nullmbr The android.app.PendingIntent to send the media button event to.

     public void More ...setMediaButtonReceiver(@Nullable PendingIntent mbr) {
         try {
              mBinder.setMediaButtonReceiver(mbr);
         } catch (RemoteException e) {
             Log.wtf(TAG, "Failure in setMediaButtonReceiver.", e);
         }
     }
like image 163
Bas van Stein Avatar answered Oct 18 '22 01:10

Bas van Stein