Hac*_*ord 7 android headset android-mediasession
我正在尝试从耳机或汽车控制(播放/暂停/等)接收媒体按钮事件
这是我的应用清单.
<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>
Run Code Online (Sandbox Code Playgroud)
这是我的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;
}
Run Code Online (Sandbox Code Playgroud)
好像我错过了一些东西.当我按下耳机控件上的暂停按钮时,永远不会调用onStartCommand.
知道为什么这不能按预期工作吗?
ian*_*ake 10
正如媒体播放I/O 2016演讲中的最佳实践中所述,您还需要打电话
mSession.setFlags(
MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
Run Code Online (Sandbox Code Playgroud)
在构建媒体会话时和开始播放之前,必须根据其文档调用setActive(true):
必须先将会话设置为活动,然后才能开始接收媒体按钮事件或传输命令.
请注意,您还必须调用setActions()在你的PlaybackStateCompat.Builder说你支持什么动作-如果你不设置,那么你不会得到任何有关媒体按钮的回调.
| 归档时间: |
|
| 查看次数: |
6586 次 |
| 最近记录: |