我有一个媒体播放器服务,带有用于播放流的自定义解码器。我尝试了所有可以在网上找到的用于在屏幕上注册媒体控制按钮的方法,但它们没有出现。
该服务的工作方式如下:
在
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); }
我还注册了一个焦点更改侦听器,但没有用。你能帮我将我的应用程序连接到小部件按钮吗?