这是MediaSessionCompat代码,仅用于媒体按钮控制,不适用于传输控制,
mediaSessionCompat=new MediaSessionCompat(this,"simple_music_button_control");
mediaSessionCallback = new MediaSessionCompat.Callback() {
@Override
public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
...
}
};
mediaSessionCompat.setCallback(mediaSessionCallback);
mediaSessionCompat.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);
mediaSessionCompat.setActive(true);
这是音频焦点代码,
...
if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
Log.e("media session",mediaSessionCompat.isActive()?"true":"false");
mediaSessionCompat.setActive(true);
if (!manual_pause) {
if (media_player_state == media_player_pausing) {
if (media_player != null)
media_player.start();
media_player_state = media_player_playing;
Intent resume_music_intent = new Intent("resume_song_intent");
LocalBroadcastManager.getInstance(ServiceMusicPlay.this).sendBroadcast(resume_music_intent);
notification = notificationMusicPlay.buildNotification(current_play_song, media_player_state);
startForeground(notification_id, notification);
}
}
}
问题是重新获得音频焦点(例如从MX Video Player重新获得音频)后,媒体控制将无法工作。重新获取音频焦点时,正在检查mediaSessionCompat,它不是null,并且isActive为true,但是media按钮将不起作用!
感谢您的帮助!