I have an media player service with custom decoders for playing streams. I tried all I could find on the web for registering media control buttons on the screen, but they don't show up.
The service works as it follows:
In
onCreate()
I did the following: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);
When the service received a play message, I start the song and I call the following code for lockscreen buttons:
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); }
I also have a focus change listener registered, but with no use. Can you help me connect my application to widget buttons?