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

前端 未结 2 891
小鲜肉
小鲜肉 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.

提交回复
热议问题