- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试从耳机或汽车控件(播放/暂停/等)接收媒体按钮事件
这在我的应用程序 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/
我是安卓开发新手。我想创建一个以客户端-服务器方式运行的音乐播放器。我现在正在尝试创建服务器部分。我发现我可以使用 MediaBrowserServiceCompat 来实现这一点,正如官方文档 he
我的服务: public class MusicService extends MediaBrowserServiceCompat { ... } 我的 Activity : public class
我创建了一个实现 MediaBrowserServiceCompat 的测试应用程序。我已遵循本指南: https://developer.android.com/guide/topics/media
如何从事件访问我在 MediaBrowserServiceCompat 中定义的公共(public)方法(不是覆盖的方法)? 之前我只使用过服务,所以我可以将它与事件类绑定(bind),然后轻松访问方
我正在开发一个 android auto,但是我在 Service 的 Onbind 方法中的这部分代码有一些问题: public IBinder onBind(Intent arg0) {
我出乎意料地努力获取派生自 MediaBrowserServiceCompat 的服务实例。 对于典型的服务,要实现这一点,使用本地绑定(bind)器 class MyService extends
我正在使用这个 example code构建一个 MediaPlayer 服务。我或多或少地工作,但是如果用户返回到包含媒体控件的 Activity,我需要检测 MediaPlayer 处于什么状态。
我正在开发一个音频播放器。播放器可通过蓝牙控制(暂停、播放……)。接下来我要实现的是通过 MediaBrowserServiceCompat 服务从播放列表中选择轨道。到目前为止,唯一的测试客户是我的
我有一个 Service 使用新的 MediaBrowserServiceCompat 与我的 Player 实现交互。 这是我的服务的 onCreate() @Override public voi
我正在尝试从耳机或汽车控件(播放/暂停/等)接收媒体按钮事件 这在我的应用程序 list 中。 这是我的 Medi
我正在尝试将媒体项目公开给可以通过我的 MediaBrowserServiceCompat 浏览我的应用程序内容的其他媒体应用程序服务。在我的 onLoadChildren我正在构建的方法 Media
我已在我的应用程序中实现了 MediaBrowserServiceCompat。 @Override public BrowserRoot onGetRoot(@NonNull String s, i
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我正在开发一款使用 Android 架构组件的音频应用。我有一个服务,它扩展了 MediaBrowserServiceCompat 和一个带有 MediaBrowserCompat 的 fragmen
我是 Android 开发的初学者,试图通过实现 MediaBrowser 和 MediaSession 创建一个媒体播放器。 我对 onGetRoot() 和 onLoadChildren() 的功
我一直在忙着更新 universal Android Mediaplayer使用 Compat 库使其在低于 lollipop 的 Android 版本上运行。 这是一个用它们的兼容变体替换大多数类的
我有一个 Android Auto 应用程序。我想利用分页在应用程序内进行浏览。看来您可以通过获取对 MediaBrowserCompat 的引用并在 .subscribe() 中传递这些常量来设置
我是一名优秀的程序员,十分优秀!