0

When we loose audio focus due to AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK we should reduce sound volume, untile audiofocus will be restored to AudioManager.AUDIOFOCUS_GAIN. However, it is fired if we registered listener before event.
How to check if audio focus is in state AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK if we registered after AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK event?

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;
          }
        }
      };
Yarh
  • 4,459
  • 5
  • 45
  • 95

1 Answers1

0

If I understand your question correctly, you want to know whether some other app is currently holding audio focus (and thus the audio output of your app should be ducked) before you actually have registered your audio focus listener.

If that's the case (some other app currently owns the audio focus), then your request for acquiring the audio focus will be denied, that is, your call to AudioManager.requestAudioFocus with audiofocusListener will return with AUDIOFOCUS_REQUEST_FAILED.

Mikhail Naganov
  • 6,643
  • 1
  • 26
  • 26