Question

I've been through the sample program for RemoteControlClient that's provided with the SDK (RandomMusicPlayer). However, I can't for the life of me figure out how to get lockscreen controls with my own music player using RemoteControlClient. Here's what I have with my music player service:

//Request audio focus for playback
int result = audioManager.requestAudioFocus(audioFocusChangeListener,
                                            AudioManager.STREAM_MUSIC,
                                            AudioManager.AUDIOFOCUS_GAIN);

//Check if audio focus was granted. If not, stop the service.
if (result!=AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
    //Stop the service.
    stopSelf();
    Toast.makeText(mContext, R.string.close_other_audio_apps, Toast.LENGTH_LONG).show();
}

ComponentName remoteControlsReceiver = new ComponentName(getPackageName(),      
                                         HeadsetButtonsReceiver.class.getName());

if (mRemoteControlClientCompat == null) {
   Intent remoteControlIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
   remoteControlIntent.setComponent(remoteControlsReceiver);

   mRemoteControlClientCompat = new RemoteControlClientCompat(PendingIntent.getBroadcast(this, 0, remoteControlIntent, 0));
   RemoteControlHelper.registerRemoteControlClient(audioManager, mRemoteControlClientCompat);
   audioManager.registerMediaButtonEventReceiver(remoteControlsReceiver);
}

This is basically what the SDK sample does (as far as I can tell). I've confirmed that my app is able to get audio focus. I've done my homework and know that audio focus is required for this to work. What exactly am I missing here? Any pointers in the right direction are much appreciated. :)

Was it helpful?

Solution

Figured it out. I was initially getting AudioFocus correctly, but there was a flaw in my code that caused it to lose AudioFocus a few seconds later. This caused the lockscreen controls to not be displayed.

Bottom line, if you can't see your controls and it looks like you have your code set up correctly, make sure you're not inadvertently calling abandonFocus(...) prematurely. That's what was causing issues for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top