Search code examples
androidandroid-audiomanager

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?


Solution

  • 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);
             }
         }