问题描述
我尝试接收使用新MediaSession类媒体按钮presses,到目前为止,我一直不成功。有没有人管理使用新类来接待他们?
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?
我已经成功地创造一个MediaSession,并用它来更新远程显示(在车载娱乐系统)上的歌曲信息,但到目前为止,我无法接收按钮presses从中,耳机控件,以及蓝牙耳机控制。
I’ve been successful in creating a MediaSession and using it to update song information on a remote display (an in-car entertainment system) but so far I’m unable to receive button presses from it, headphone controls, and controls on Bluetooth headsets.
在我创建我执行的,我用它来播放音频服务下面的媒体会话:
After I create the media session I’m executing the following in a service that I use to play audio:
_mediaSession = new MediaSession(getApplicationContext(), Global.PACKAGE_NAME + "." + TAG);
if (_mediaSession == null) {
_log.e(TAG, "initMediaSession: _mediaSession = null");
return;
}
_mediaSessionToken = _mediaSession.getSessionToken();
_mediaSession.setCallback(new Callback() {
public boolean onMediaButtonEvent(Intent mediaButtonIntent) {
_log.d(TAG, "onMediaButtonEvent called: " + mediaButtonIntent);
return false;
}
public void onPause() {
Log.d(TAG, "onPause called (media button pressed)");
super.onPause();
}
public void onPlay() {
Log.d(TAG, "onPlay called (media button pressed)");
super.onPlay();
}
public void onStop() {
Log.d(TAG, "onStop called (media button pressed)");
super.onStop();
}
});
_mediaSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
PlaybackState state = new PlaybackState.Builder()
.setActions(PlaybackState.ACTION_PLAY)
.setState(PlaybackState.STATE_STOPPED, PlaybackState.PLAYBACK_POSITION_UNKNOWN, SystemClock.elapsedRealtime())
.build();
_mediaSession.setPlaybackState(state);
_mediaSession.setActive(true);
但我仍然没有收到任何按钮presses。
but I’m still not receiving any button presses.
任何人有什么想法?
感谢
更新变更后
PlaybackState state = new PlaybackState.Builder()
.setActions(PlaybackState.ACTION_PLAY)
.setState(PlaybackState.STATE_STOPPED, PlaybackState.PLAYBACK_POSITION_UNKNOWN, SystemClock.elapsedRealtime())
.build();
到
PlaybackState state = new PlaybackState.Builder()
.setActions(PlaybackState.ACTION_PLAY)
.setState(PlaybackState.STATE_STOPPED, PlaybackState.PLAYBACK_POSITION_UNKNOWN, 0)
.build();
我现在通过onMediaButtonEvent()回调接收按钮preSS通知(如被通知KEY code_MEDIA_PAUSE是pressed),但onPlay(),在onPause(),和的onStop()永远不会被调用,任何想法,为什么?
I'm now receiving button press notifications via the onMediaButtonEvent() callback (such as being notified that KEYCODE_MEDIA_PAUSE was pressed) but onPlay(), onPause(), and onStop() are never being called, any idea why?
推荐答案
onMediaButtonEvent(..)
在MediaSession.Callback的默认实现。在您的code,如果你调用super.onMediaButtonEvent(..),然后根据不同的按键codeS正确的回调即 onPlay()
,的onPause()
将被调用。
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.
您可以看看在 MediaSession.java
这篇关于如何使用新的MediaSession类接收媒体按钮presses在Android 5.x的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!