openAudioSession method

Future<FlutterSoundPlayer?> openAudioSession({
  1. AudioFocus focus = AudioFocus.requestFocusAndKeepOthers,
  2. SessionCategory category = SessionCategory.playAndRecord,
  3. SessionMode mode = SessionMode.modeDefault,
  4. AudioDevice device = AudioDevice.speaker,
  5. int audioFlags = outputToSpeaker | allowBlueToothA2DP | allowAirPlay,
  6. bool withUI = false,
})

Open the Player.

A player must be opened before used. A player correspond to an Audio Session. With other words, you must open the Audio Session before using it. When you have finished with a Player, you must close it. With other words, you must close your Audio Session. Opening a player takes resources inside the OS. Those resources are freed with the verb closeAudioSession(). Returns a Future, but the App does not need to wait the completion of this future before doing a startPlayer(). The Future will be automaticaly waited by startPlayer()

  • focus : What to do with others App if they have already the Focus
  • category : An optional parameter for iOS. See iOS documentation.
  • mode : an optional parameter for iOS. See iOS documentation to understand the meaning of this parameter.
  • audioFlags : an optional parameter for iOS
  • withUI : true if the App plan to use closeAudioSession later.

Example:

    myPlayer = await FlutterSoundPlayer().closeAudioSession(focus: Focus.requestFocusAndDuckOthers, outputToSpeaker | allowBlueTooth);

    ...
    (do something with myPlayer)
    ...

    await myPlayer.closeAudioSession();
    myPlayer = null;

Implementation

Future<FlutterSoundPlayer?> openAudioSession({
  AudioFocus focus = AudioFocus.requestFocusAndKeepOthers,
  SessionCategory category = SessionCategory.playAndRecord,
  SessionMode mode = SessionMode.modeDefault,
  AudioDevice device = AudioDevice.speaker,
  int audioFlags = outputToSpeaker | allowBlueToothA2DP | allowAirPlay,
  bool withUI = false,
}) async {
  if (_isInited != Initialized.notInitialized) {
    return this;
  }
  FlutterSoundPlayer? r;
  await _lock.synchronized(() async {
    r = await _openAudioSession(
      focus: focus,
      category: category,
      mode: mode,
      device: device,
      audioFlags: audioFlags,
      withUI: withUI,
    );
  });
  return r;
}