因此,我使用以下代码尝试在兼容蓝牙 AVRCP 的设备上显示元数据,但到目前为止还没有任何运气。
锁屏上没有出现任何控件,这让我相信 RemoteControlClient` 没有按预期工作。
AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
ComponentName rec = new ComponentName(getPackageName(), MediaButtonReceiver.class.getName());
mAudioManager.registerMediaButtonEventReceiver(rec);
Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
i.setComponent(rec);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
RemoteControlClient 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;
mRemoteControlClient.setTransportControlFlags(flags);
mAudioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
RemoteControlClient.MetadataEditor mEditor = mRemoteControlClient.editMetadata(true);
mEditor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, "RCC Artist");
mEditor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, "RCC Title");
mEditor.putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, "RCC Album");
mEditor.putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, 6039000);
mEditor.apply();
mRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
当按下按钮时,我正在执行此代码。