我有一个媒体播放器服务,带有用于播放流的自定义解码器。我尝试了在网络上找到的所有内容,用于在屏幕上注册媒体控制按钮,但它们没有显示出来。
该服务的工作原理如下:
在onCreate()
我做了以下事情:
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Intent buttonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
ComponentName component = new ComponentName(this, MediaControlReceiver.class);
buttonIntent.setComponent(component);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, buttonIntent, 0);
myRemoteControlClient = new RemoteControlClient(pendingIntent);
audioManager.registerRemoteControlClient(myRemoteControlClient);
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;
myRemoteControlClient.setTransportControlFlags(flags);
当服务收到播放消息时,我启动歌曲,并为锁屏按钮调用以下代码:
int result = audioManager.requestAudioFocus(focusChangeListener,
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
Log.d("RemoteControl", "Status: PLAY" + result);
myRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
setRemoteControlMetadata(null, "test", "test", 1);
}
我也注册了焦点更改侦听器,但没有用。你能帮我把我的应用程序连接到widget按钮吗?