setActive method Null safety

Future<bool> setActive(
  1. bool active,
  2. {AVAudioSessionSetActiveOptions? avAudioSessionSetActiveOptions,
  3. AndroidAudioFocusGainType? androidAudioFocusGainType,
  4. AndroidAudioAttributes? androidAudioAttributes,
  5. bool? androidWillPauseWhenDucked,
  6. AudioSessionConfiguration fallbackConfiguration = const AudioSessionConfiguration.music()}
)

Activates or deactivates this audio session. Typically an audio plugin should call this method when it begins playing audio. If the audio session is not yet configured at the time this is called, the fallbackConfiguration will be set. If any of avAudioSessionSetActiveOptions, androidAudioFocusGainType, androidAudioAttributesttributes and androidWillPauseWhenDucked are speficied, they will override the configuration.

Implementation

Future<bool> setActive(
  bool active, {
  AVAudioSessionSetActiveOptions? avAudioSessionSetActiveOptions,
  AndroidAudioFocusGainType? androidAudioFocusGainType,
  AndroidAudioAttributes? androidAudioAttributes,
  bool? androidWillPauseWhenDucked,
  AudioSessionConfiguration fallbackConfiguration =
      const AudioSessionConfiguration.music(),
}) async {
  final configuration = _configuration ?? fallbackConfiguration;
  if (!isConfigured) {
    await configure(fallbackConfiguration);
  }
  if (!kIsWeb && Platform.isIOS) {
    return await _avAudioSession!.setActive(active,
        avOptions: avAudioSessionSetActiveOptions ??
            configuration.avAudioSessionSetActiveOptions);
  } else if (!kIsWeb && Platform.isAndroid) {
    if (active) {
      // Activate
      final pauseWhenDucked =
          configuration.androidWillPauseWhenDucked ?? false;
      var ducked = false;
      final success = await _androidAudioManager!
          .requestAudioFocus(AndroidAudioFocusRequest(
        gainType: androidAudioFocusGainType ??
            configuration.androidAudioFocusGainType,
        audioAttributes:
            androidAudioAttributes ?? configuration.androidAudioAttributes,
        willPauseWhenDucked: androidWillPauseWhenDucked ??
            configuration.androidWillPauseWhenDucked,
        onAudioFocusChanged: (focus) {
          print("core onAudioFocusChanged");
          switch (focus) {
            case AndroidAudioFocus.gain:
              _interruptionEventSubject.add(AudioInterruptionEvent(
                  false,
                  ducked
                      ? AudioInterruptionType.duck
                      : AudioInterruptionType.pause));
              ducked = false;
              break;
            case AndroidAudioFocus.loss:
              _interruptionEventSubject.add(AudioInterruptionEvent(
                  true, AudioInterruptionType.unknown));
              ducked = false;
              break;
            case AndroidAudioFocus.lossTransient:
              _interruptionEventSubject.add(
                  AudioInterruptionEvent(true, AudioInterruptionType.pause));
              ducked = false;
              break;
            case AndroidAudioFocus.lossTransientCanDuck:
              // We enforce the "will pause when ducked" configuration by
              // sending the app a pause event instead of a duck event.
              _interruptionEventSubject.add(AudioInterruptionEvent(
                  true,
                  pauseWhenDucked
                      ? AudioInterruptionType.pause
                      : AudioInterruptionType.duck));
              if (!pauseWhenDucked) ducked = true;
              break;
          }
        },
      ));
      return success;
    } else {
      // Deactivate
      final success = await _androidAudioManager!.abandonAudioFocus();
      return success;
    }
  }
  return true;
}