编辑 2016 年 5 月 8 日
我找到了无法在我的应用程序中接收媒体按钮事件的原因。请参阅下面的答案。我编辑了这个问题的标题,以便更容易找到问题。最初的标题是“什么可能会阻止 Android Lollipop 上的媒体按钮”。
原始问题,2015 年 4 月:
抓挠我的头,盯着所有的代码看了 2 天无济于事......我的 Android 应用程序应该对媒体按钮做出反应(例如,从耳机,用蓝牙耳机测试它),比如播放/暂停、下一个、倒带. 在 KitKat 及以下版本上运行良好。我发誓它甚至在几天前也适用于 Lollipop。现在什么也没有,它没有听到媒体按钮按下的痕迹。有人可以快速建议在哪里寻找麻烦吗?使用两部 Lollipop 手机、相同的蓝牙耳机和相同的耳机进行测试,适用于较低版本的 Android。同样的耳机也可以正常工作,在其他应用程序中听到媒体按钮按下。我能打破什么???
我现在测试了聆听媒体按钮的新旧方式。在 AndroidManifest.xml 中:
<receiver android:name=".MediaButtonIntentReceiver" android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
它说 enabled="false" 的事实是好的 - 我根据需要启用和禁用接收器,MediaButtonIntentReceiver.java 在 KitKat 上获得良好的事件,在 Lollipop 上获得更低的完全静音。
接下来我切换到最新的 appcompat (v22.1) 并尝试使用 MediaSessionCompat 对象和相关代码,如下所示。这在一个小型测试应用程序中效果很好,我只写了一个活动 - 让我的 Logcat 消息确认它听到了棒棒糖上按下的媒体键。但是当插入我的应用程序时,再次无法在 Lollipop 上运行。有没有搞错???
private MediaSessionCompat _mediaSession;
final String MEDIA_SESSION_TAG = "some_tag";
void initMediaSessions(Context context) {
// Start a new MediaSession
if (context == null)
return;
Lt.d("initMediaSession()...");
ComponentName eventReceiver = new ComponentName(context.getPackageName(), MediaButtonIntentReceiver.class.getName());
PendingIntent buttonReceiverIntent = PendingIntent.getBroadcast(
context,
0,
new Intent(Intent.ACTION_MEDIA_BUTTON),
PendingIntent.FLAG_UPDATE_CURRENT
);
// Parameters for new MediaSessionCompat():
// context The context.
// tag A short name for debugging purposes.
// mediaButtonEventReceiver The component name for your receiver. This must be non-null to support platform
// versions earlier than LOLLIPOP. May not be null below Lollipop.
// mbrIntent The PendingIntent for your receiver component that handles media button events. This is optional
// and will be used on JELLY_BEAN_MR2 and later instead of the component name.
_mediaSession = new MediaSessionCompat(context, MEDIA_SESSION_TAG, eventReceiver, buttonReceiverIntent);
_mediaSession.setCallback(new MediaSessionCallback());
_mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
_mediaSession.setActive(true);
PlaybackStateCompat state = new PlaybackStateCompat.Builder()
.setActions(
PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_PAUSE |
PlaybackStateCompat.ACTION_PAUSE |
PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
.setState(PlaybackStateCompat.STATE_STOPPED, 0, 1, SystemClock.elapsedRealtime())
.build();
_mediaSession.setPlaybackState(state);
}
final class MediaSessionCallback extends MediaSessionCompat.Callback {
@Override
public void onPlay() {
Lt.d("play");
}
@Override
public void onPause() {
Lt.d("pause");
}
@Override
public void onStop() {
Lt.d("stop.");
}
@Override
public void onSkipToNext() {
Lt.d("skipToNext");
}
@Override
public void onSkipToPrevious() {
Lt.d("skipToPrevious");
}
@Override
public boolean onMediaButtonEvent(final Intent mediaButtonIntent) {
Lt.d("GOT MediaButton EVENT");
KeyEvent keyEvent = (KeyEvent) mediaButtonIntent.getExtras().get(Intent.EXTRA_KEY_EVENT);
// ...do something with keyEvent, super... does nothing.
return super.onMediaButtonEvent(mediaButtonIntent);
}
}