openAudioSession method

Future<FlutterSoundRecorder?> openAudioSession({
  1. AudioFocus focus = AudioFocus.requestFocusTransient,
  2. SessionCategory category = SessionCategory.playAndRecord,
  3. SessionMode mode = SessionMode.modeDefault,
  4. int audioFlags = outputToSpeaker,
  5. AudioDevice device = AudioDevice.speaker,
})

Open a Recorder

A recorder must be opened before used. A recorder correspond to an Audio Session. With other words, you must open the Audio Session before using it. When you have finished with a Recorder, you must close it. With other words, you must close your Audio Session. Opening a recorder takes resources inside the OS. Those resources are freed with the verb closeAudioSession().

You MUST ensure that the recorder has been closed when your widget is detached from the UI. Overload your widget's dispose() method to close the recorder when your widget is disposed. In this way you will reset the Recorder and clean up the device resources, but the recorder will be no longer usable.

@override
void dispose()
{
        if (myRecorder != null)
        {
            myRecorder.closeAudioSession();
            myRecorder = null;
        }
        super.dispose();
}

You may not openAudioSession many recorders without releasing them.

openAudioSession() and closeAudioSession() return Futures. You do not need to wait the end of the initialization before startRecorder(). startRecorder will automaticaly wait the end of openAudioSession() before starting the recorder.

The four optional parameters are used if you want to control the Audio Focus. Please look to FlutterSoundRecorder openAudioSession() to understand the meaning of those parameters

Example:

    myRecorder = await FlutterSoundRecorder().openAudioSession();

    ...
    (do something with myRecorder)
    ...

    myRecorder.closeAudioSession();
    myRecorder = null;

Implementation

Future<FlutterSoundRecorder?> openAudioSession(
    {AudioFocus focus = AudioFocus.requestFocusTransient,
    SessionCategory category = SessionCategory.playAndRecord,
    SessionMode mode = SessionMode.modeDefault,
    int audioFlags = outputToSpeaker,
    AudioDevice device = AudioDevice.speaker}) async {
  if (_isInited != Initialized.notInitialized) {
    return this;
  }

  FlutterSoundRecorder? r;
  _logger.d('FS:---> openAudioSession ');
  await _lock.synchronized(() async {
    r = await _openAudioSession(
      focus: focus,
      category: category,
      mode: mode,
      audioFlags: audioFlags,
      device: device,
    );
  });
  _logger.d('FS:<--- openAudioSession ');
  return r;
}