openRecorder method

Future<FlutterSoundRecorder?> openRecorder({
  1. dynamic isBGService = false,
})

Open a Recorder

A recorder must be opened before used. Opening a recorder takes resources inside the OS. Those resources are freed with the verb closeRecorder().

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.closeRecorder();
            myRecorder = null;
        }
        super.dispose();
}

You may not openAudioSession many recorders without releasing them.

openRecorder() and closeRecorder() return Futures. You do not need to wait the end of the initialization before startRecorder(). startRecorder will automaticaly wait the end of openRecorder() 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().openRecorder();

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

    myRecorder.closeRecorder();
    myRecorder = null;

Implementation

Future<FlutterSoundRecorder?> openRecorder({isBGService = false}) async {
  if (_isInited != Initialized.notInitialized) {
    return this;
  }

  if (isBGService) {
    await MethodChannel("xyz.canardoux.flutter_sound_bgservice")
        .invokeMethod("setBGService");
  }

  Future<FlutterSoundRecorder?>? r;
  _logger.d('FS:---> openAudioSession ');
  await _lock.synchronized(() async {
    r = _openAudioSession();
  });
  _logger.d('FS:<--- openAudioSession ');
  return r;
}