2

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

4

1 に答える 1

6

理解した。最初は AudioFocus を正しく取得していましたが、コードに欠陥があり、数秒後に AudioFocus が失われました。これにより、ロック画面のコントロールが表示されなくなりました。

要するに、コントロールが表示されず、コードが正しく設定されているように見える場合は、誤ってabandonFocus(...)時期尚早に呼び出していないことを確認してください。それが私にとって問題を引き起こしていたものです。

于 2013-12-26T22:26:38.070 に答える