gpt4 book ai didi

android - MediaButtonReceiver 不适用于 MediaBrowserServiceCompat

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:22:47 24 4
gpt4 key购买 nike

我正在尝试从耳机或汽车控件(播放/暂停/等)接收媒体按钮事件

这在我的应用程序 list 中。

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

构建媒体 session 时和开始播放之前,必须调用 setActive(true)根据其文档:

You must set the session to active before it can start receiving media button events or transport commands.

请注意,您还必须 call setActions()在你的 PlaybackStateCompat.Builder准确说明您支持的操作 - 如果您不设置它,那么您将不会收到任何与媒体按钮相关的回调。

关于android - MediaButtonReceiver 不适用于 MediaBrowserServiceCompat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38247050/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com