How to use the new MediaSession class to receive media button presses on Android 5.x?

前端 未结 2 864
小鲜肉
小鲜肉 2020-12-14 23:04

I’m attempting to receive media button presses using the new MediaSession class and so far I’ve been unsuccessful. Has anyone managed to receive them using the new class?

相关标签:
2条回答
  • 2020-12-14 23:46

    The default implementation of onMediaButtonEvent is what figures out the specific callback for a given media key event. Since you're overriding onMediaButtonEvent and not calling the super's implementation you will only get onPlay/Pause/etc. calls from other apps that are using a MediaController to make those calls directly.

    If you add change your implementation to

    public boolean onMediaButtonEvent(Intent mediaButtonIntent) {
        _log.d(TAG, "onMediaButtonEvent called: " + mediaButtonIntent);             
        return super.onMediaButtonEvent(mediaButtonIntent);
    }
    

    You should start seeing the keys translated to the other callback methods.

    0 讨论(0)
  • 2020-12-14 23:52

    onMediaButtonEvent(..) has a default implementation in MediaSession.Callback. In your code if you call super.onMediaButtonEvent(..), then depending on the keycodes the correct callback namely onPlay(), onPause() will be called.

    You can take a look at the default implementation in MediaSession.java

    0 讨论(0)
提交回复
热议问题