I am currently creating a streaming Android application and I am trying to integrate a remote control client (for example, to control from the lock screen on ICS +).
To do this, I do this at startup in my streaming service:
int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { stopSelf(); } mediaButtonReceiverComponent = new ComponentName(this, RemoteControlReceiver.class); audioManager.registerMediaButtonEventReceiver(mediaButtonReceiverComponent); if (remoteControlClientCompat == null) { final Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); mediaButtonIntent.setComponent(mediaButtonReceiverComponent); remoteControlClientCompat = new RemoteControlClientCompat( PendingIntent.getBroadcast( getApplicationContext(), 0, mediaButtonIntent, PendingIntent.FLAG_UPDATE_CURRENT ) ); RemoteControlHelper.registerRemoteControlClient(audioManager, remoteControlClientCompat); } final int flags = RemoteControlClient.FLAG_KEY_MEDIA_STOP; remoteControlClientCompat.setTransportControlFlags(flags);
remoteControlClientCompat is just an instance of RemoteControlClientCompat from the samples.
then during streaming I update the metadata. everything works fine, even the controls are sent to my RemoteControlReceiver . Data and image are displayed on the lock screen.
Stopping streaming from my application destroys the lock object, but when I try to destroy it from the widget itself (by clicking the stop button), it does something strange. Pressing the stop button makes the broadcast receiver a great streaming service for me. Then in the onDestroy () service method, I do this:
RemoteControlHelper.unregisterRemoteControlClient(audioManager, remoteControlClientCompat); audioManager.unregisterMediaButtonEventReceiver(mediaButtonReceiverComponent); audioManager.abandonAudioFocus(this);
The widget blinks as soon as audioManager.unregisterMediaButtonEventReceiver(mediaButtonReceiverComponent); . I tried commenting out a line and blinking happens with audioManager.abandonAudioFocus(this); . Commenting out that the other line is also blinking when the service stops.
I noticed that this also happens when I stop transmitting from my notification.
What am I doing wrong? I tried to reorder these calls, but I could not solve it. I noticed that Spotify has the same problem a couple of versions back. I wonder how they solved it ...