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. :)