如何检查是否应回避声音?

How to check if sound should be ducked?

当我们由于AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK而失去音频焦点时,我们应该降低音量,直到音频焦点恢复到AudioManager.AUDIOFOCUS_GAIN。但是,如果我们在事件之前注册监听器,它就会被触发。
如果我们在 AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK 事件之后注册,如何检查音频焦点是否处于状态 AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK

AudioManager.OnAudioFocusChangeListener audiofocusListener =
      new AudioManager.OnAudioFocusChangeListener() {
        @Override public void onAudioFocusChange(int focusChange) {
          switch (focusChange) {
            case AudioManager.AUDIOFOCUS_GAIN: {
            }
            break;
            case AudioManager.AUDIOFOCUS_LOSS: {
            }
            break;
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: {
            }
            break;
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: {
            }
            break;
          }
        }
      };

如果我对你的问题的理解正确,你想知道在你实际注册你的音频焦点监听器之前是否有其他应用程序当前持有音频焦点(因此你的应用程序的音频输出应该被回避)。

如果是这样(其他应用当前拥有音频焦点),那么您获取音频焦点的请求将被拒绝,即您使用audiofocusListener调用AudioManager.requestAudioFocus将return 与 AUDIOFOCUS_REQUEST_FAILED.