MediaButtonReceiver 不适用于 MediaBrowserServiceCompat
MediaButtonReceiver not working with MediaBrowserServiceCompat
我正在尝试从耳机或汽车控件接收媒体按钮事件 (play/pause/etc。)
这在我的应用清单中。
<service android:name=".mediaPlayerService.MediaPlayerService"
android:exported="true">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService"/>
</intent-filter>
</service>
<!--
A receiver that will receive media buttons and send as
intents to your MediaBrowserServiceCompat implementation.
Required on pre-Lollipop. More information at
http://developer.android.com/reference/android/support/v4/media/session/MediaButtonReceiver.html
-->
<receiver android:name="android.support.v4.media.session.MediaButtonReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON"/>
</intent-filter>
</receiver>
这是我的 MediaPlayerService 的一部分
public class MediaPlayerService extends MediaBrowserServiceCompat {
@Override
public void onCreate()
{
super.onCreate();
initMediaSessions();
}
private void initMediaSessions()
{
mSession = new MediaSessionCompat(getApplicationContext(), MediaPlayerService.class.getSimpleName());
setSessionToken(mSession.getSessionToken());
mSession.setCallback(new MediaSessionCompat.Callback()
{
//callback code is here.
}
);
}
@Override
public int onStartCommand(Intent startIntent, int flags, int startId)
{
if (startIntent != null)
{
// Try to handle the intent as a media button event wrapped by MediaButtonReceiver
MediaButtonReceiver.handleIntent(mSession, startIntent);
}
return START_STICKY;
}
我好像漏掉了什么。当我按下耳机控件上的暂停按钮时,永远不会调用 onStartCommand。
知道为什么这没有按预期工作吗?
如 Best Practices in media playback I/O 2016 talk 中所述,您还需要调用
mSession.setFlags(
MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
构建媒体会话时和开始播放之前,您必须根据其文档调用 setActive(true):
You must set the session to active before it can start receiving media button events or transport commands.
请注意,您还必须 call setActions() on your PlaybackStateCompat.Builder 准确说明您支持的操作 - 如果您不进行设置,那么您将不会收到任何与媒体按钮相关的回调。
我正在尝试从耳机或汽车控件接收媒体按钮事件 (play/pause/etc。)
这在我的应用清单中。
<service android:name=".mediaPlayerService.MediaPlayerService"
android:exported="true">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService"/>
</intent-filter>
</service>
<!--
A receiver that will receive media buttons and send as
intents to your MediaBrowserServiceCompat implementation.
Required on pre-Lollipop. More information at
http://developer.android.com/reference/android/support/v4/media/session/MediaButtonReceiver.html
-->
<receiver android:name="android.support.v4.media.session.MediaButtonReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON"/>
</intent-filter>
</receiver>
这是我的 MediaPlayerService 的一部分
public class MediaPlayerService extends MediaBrowserServiceCompat {
@Override
public void onCreate()
{
super.onCreate();
initMediaSessions();
}
private void initMediaSessions()
{
mSession = new MediaSessionCompat(getApplicationContext(), MediaPlayerService.class.getSimpleName());
setSessionToken(mSession.getSessionToken());
mSession.setCallback(new MediaSessionCompat.Callback()
{
//callback code is here.
}
);
}
@Override
public int onStartCommand(Intent startIntent, int flags, int startId)
{
if (startIntent != null)
{
// Try to handle the intent as a media button event wrapped by MediaButtonReceiver
MediaButtonReceiver.handleIntent(mSession, startIntent);
}
return START_STICKY;
}
我好像漏掉了什么。当我按下耳机控件上的暂停按钮时,永远不会调用 onStartCommand。
知道为什么这没有按预期工作吗?
如 Best Practices in media playback I/O 2016 talk 中所述,您还需要调用
mSession.setFlags(
MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
构建媒体会话时和开始播放之前,您必须根据其文档调用 setActive(true):
You must set the session to active before it can start receiving media button events or transport commands.
请注意,您还必须 call setActions() on your PlaybackStateCompat.Builder 准确说明您支持的操作 - 如果您不进行设置,那么您将不会收到任何与媒体按钮相关的回调。