[android]AUDIOFOCUS_GAIN 不叫后 AUDIOFOCUS_LOSS_TRANSIENT 后打个电话

标签: Java Android
发布时间: 2016/10/10 21:01:06
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有一个典型 onAudioFocusChange 代码在我的游戏。

@Override
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {
        case AudioManager.AUDIOFOCUS_GAIN:
            Log.i(TAG,"AUDIOFOCUS_GAIN");
            mHasAudioFocus = requestAudioFocus();
            break;
        case AudioManager.AUDIOFOCUS_LOSS:
            Log.i(TAG,"AUDIOFOCUS_LOSS");
            abandonAudioFocus();
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
            Log.i(TAG,"AUDIOFOCUS_LOSS_TRANSIENT");
            abandonAudioFocus();
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
            Log.i(TAG,"AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
            abandonAudioFocus();
            break;
        default:
            Log.i(TAG,"Unrecognized audiofocus change");
    }

问题在于,当一个浮 window 出现在三星手机上 application 接收 AUDIOFOCUS_LOSS_TRANSIENT 消息,但是当这 window 是闭合的没有消息来到 application

enter image description here

这里是有关 log 行︰

13:20:53.253   770  7745 I MediaFocusControl:  AudioFocus  requestAudioFocus() from AudioFocus_For_Phone_Ring_And_Calls
13:20:53.253 14499 14499 I com.js.helpers.JSNativeActivity: AUDIOFOCUS_LOSS_TRANSIENT
13:20:53.253 14499 14499 I com.js.helpers.JSNativeActivity: abandonAudioFocus()
13:20:53.253 14499 14499 I JSNativeActivity: onAbandonAudioFocus
13:20:53.253   770  1508 I MediaFocusControl:  AudioFocus  abandonAudioFocus() from android.media.AudioManager$42599770com.js.helpers.JSNativeActivity$4252c788
13:20:53.253   770  1508 I MediaFocusControl: AudioFocus  removeFocusStackEntry(): removing entry for android.media.AudioManager$42599770com.js.helpers.JSNativeActivity$4252c788
13:21:01.383   770  1220 I MediaFocusControl:  AudioFocus  abandonAudioFocus() from AudioFocus_For_Phone_Ring_And_Calls

任何想法如何捕捉闭幕的这 window 吗?

解决方法 1:

关键的一句是在这里
注︰ 系统将不授予音频焦点 (AUDIOFOCUS_REQUEST_FAILED) 如果是打个电话给目前在 processapplication 将不会收到 AUDIOFOCUS_GAIN,调用结束后。

所以,这是预期的行为,和我决定创建一个定时器,只是要求音频焦点,当它结束的时候。

我当前的代码是︰

@Override
public void onAudioFocusChange(int focusChange) {
    switch (focusChange) {
        case AudioManager.AUDIOFOCUS_GAIN:
            Log.d(TAG, "AUDIOFOCUS_GAIN");
            mHasAudioFocus = requestAudioFocus();
            break;
        case AudioManager.AUDIOFOCUS_LOSS:
            Log.d(TAG, "AUDIOFOCUS_LOSS");
            abandonAudioFocus();
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
            Log.d(TAG, "AUDIOFOCUS_LOSS_TRANSIENT");
            abandonAudioFocus();
            startAudioFocusCountdownTimer(); 
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
            Log.d(TAG, "AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
            abandonAudioFocus();
            startAudioFocusCountdownTimer(); 
            break;
        default:
            Log.d(TAG, "Unrecognized audiofocus change");
    }
}

private void startAudioFocusCountdownTimer() {
     new CountDownTimer(2000, 2000) {
        public void onTick(long millisUntilFinished) {
            // do nothing
        }
        public void onFinish() {
            mHasAudioFocus = requestAudioFocus();
            if (!mHasAudioFocus)
                startAudioFocusCountdownTimer();
        }
    }.start();
}
赞助商