当电话结束时,onAudioFocusChange会发生什么

时间:2017-01-20 11:17:16

标签: android

由于这个问题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();
    }
}

1 个答案:

答案 0 :(得分:2)

abandonAudioFocus()AudioManager.AUDIOFOCUS_LOSS_TRANSIENTAudioManager.AUDIOFOCUS_LOSS时,它不应该自动发生(音频焦点传递给请求音频焦点的新应用程序,你只需得到回调并且你没有放弃音频焦点请求,因为在你打电话的情况下你想再次播放你的音乐,你应该做的是AudioManager.AUDIOFOCUS_LOSS,停止你的MediaPlayer这就是AudioManager.AUDIOFOCUS_LOSS_TRANSIENT暂停MediaPlayer的所有情况,当电话结束时,您会自动拨打AudioManager.AUDIOFOCUS_GAIN,然后再次播放MediaPlayer 1}}。