由于这个问题AUDIOFOCUS_GAIN doesn't called after AUDIOFOCUS_LOSS_TRANSIENT after a phone call
,我有点困惑我不相信"忙碌的等待"解。我认为AudioManager将状态更改为AUDIOFOCUS_GAIN
此问题支持我的观点AUDIOFOCUS_LOSS called after a phone call in android
但是当我尝试时,永远不会出现AUDIOFOCUS_GAIN
的情况 AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
发送的请求
am.requestAudioFocus(af, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
和
OnAudioFocusChangeListener af = this;
和
public void onAudioFocusChange(int i) {
Log.d("AudioChanged", i + "");
if (i == AudioManager.AUDIOFOCUS_LOSS) {
am.abandonAudioFocus(this);
sendBroadcast(new Intent(Constants.ACTION.PLAY_ACTION));
} else if (i == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
am.abandonAudioFocus(this);
sendBroadcast(new Intent(Constants.ACTION.PLAY_ACTION));
Log.d("loss transient", "AUDIOFOCUS_LOSS_TRANSIENT");
} else if (i == AudioManager.AUDIOFOCUS_GAIN) {
Log.d("gain ", "AUDIOFOCUS_GAIN");
startPlaying();
}
}
答案 0 :(得分:2)
abandonAudioFocus()
当AudioManager.AUDIOFOCUS_LOSS_TRANSIENT
或AudioManager.AUDIOFOCUS_LOSS
时,它不应该自动发生(音频焦点传递给请求音频焦点的新应用程序,你只需得到回调并且你没有放弃音频焦点请求,因为在你打电话的情况下你想再次播放你的音乐,你应该做的是AudioManager.AUDIOFOCUS_LOSS
,停止你的MediaPlayer
这就是AudioManager.AUDIOFOCUS_LOSS_TRANSIENT
暂停MediaPlayer
的所有情况,当电话结束时,您会自动拨打AudioManager.AUDIOFOCUS_GAIN
,然后再次播放MediaPlayer
1}}。