我正在开发音乐播放器,并试图通过车载蓝牙处理播放器,但是我无法确定何时关闭播放器。 MEDIA_BUTTON接收器正在控制事件。
我试图通过蓝牙断开连接操作检测到此操作,但是当汽车打开且汽车播放器关闭时,蓝牙未断开连接。
在清单中:
<receiver android:name=".broadcast.RemoteControlReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
在MusicPlayerService中(在创建时):
mAudioManager.registerMediaButtonEventReceiver(
mRemoteControlResponder);
在MusicPlayerService中(在创建时):
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mRemoteControlResponder = new ComponentName(getPackageName(),
RemoteControlReceiver.class.getName());
Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
i.setComponent(mRemoteControlResponder);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
mRemoteControlClient = new RemoteControlClient(pi);
mAudioManager.registerRemoteControlClient(mRemoteControlClient);
int flags = RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
| RemoteControlClient.FLAG_KEY_MEDIA_NEXT
| RemoteControlClient.FLAG_KEY_MEDIA_PLAY
| RemoteControlClient.FLAG_KEY_MEDIA_PAUSE
| RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
| RemoteControlClient.FLAG_KEY_MEDIA_STOP
| RemoteControlClient.FLAG_KEY_MEDIA_FAST_FORWARD
| RemoteControlClient.FLAG_KEY_MEDIA_REWIND
| RemoteControlClient.FLAG_KEY_MEDIA_POSITION_UPDATE
| RemoteControlClient.FLAG_KEY_MEDIA_RATING
| RemoteControlClient.PLAYSTATE_BUFFERING
| RemoteControlClient.PLAYSTATE_ERROR
| RemoteControlClient.PLAYSTATE_FAST_FORWARDING
| RemoteControlClient.PLAYSTATE_PAUSED
| RemoteControlClient.PLAYSTATE_PLAYING
| RemoteControlClient.PLAYSTATE_REWINDING
| RemoteControlClient.PLAYSTATE_SKIPPING_BACKWARDS
| RemoteControlClient.PLAYSTATE_SKIPPING_FORWARDS
| RemoteControlClient.PLAYSTATE_STOPPED;
mRemoteControlClient.setTransportControlFlags(flags);
在MusicPlayerService中(onTracksChanged):
RemoteControlClient.MetadataEditor ed = mRemoteControlClient.editMetadata(true);
ed.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, songNamesMap.get(currentPos));
ed.putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, albumNamesMap.get(currentPos));
ed.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, artistName);
ed.apply();
我希望这种方法能够提醒关闭汽车玩家,但不会