startRecorder method

Future<void> startRecorder(
  1. {Codec codec = Codec.defaultCodec,
  2. String? toFile,
  3. StreamSink<Food>? toStream,
  4. int sampleRate = 16000,
  5. int numChannels = 1,
  6. int bitRate = 16000,
  7. int bufferSize = 8192,
  8. bool enableVoiceProcessing = false,
  9. AudioSource audioSource = AudioSource.defaultSource}
)

startRecorder() starts recording with an open session.

If an openAudioSession() is in progress, startRecorder() will automatically wait the end of the opening. startRecorder() has the destination file path as parameter. It has also 7 optional parameters to specify :

  • codec: The codec to be used. Please refer to the Codec compatibility Table to know which codecs are currently supported.
  • toFile: a path to the file being recorded or the name of a temporary file (without slash '/').
  • toStream: if you want to record to a Dart Stream. Please look to the following notice. This new functionnality needs, at least, Android SDK >= 21 (23 is better)
  • sampleRate: The sample rate in Hertz
  • numChannels: The number of channels (1=monophony, 2=stereophony)
  • bitRate: The bit rate in Hertz
  • audioSource : possible value is :
    • defaultSource
    • microphone
    • voiceDownlink (if someone can explain me what it is, I will be grateful ;-) )

path_provider can be useful if you want to get access to some directories on your device. To record a temporary file, the App can specify the name of this temporary file (without slash) instead of a real path.

Flutter Sound does not take care of the recording permission. It is the App responsability to check or require the Recording permission. Permission_handler is probably useful to do that.

Example:

    // Request Microphone permission if needed
    PermissionStatus status = await Permission.microphone.request();
    if (status != PermissionStatus.granted)
            throw RecordingPermissionException("Microphone permission not granted");

    await myRecorder.startRecorder(toFile: 'foo', codec: t_CODEC.CODEC_AAC,); // A temporary file named 'foo'

Implementation

Future<void> startRecorder({
  Codec codec = Codec.defaultCodec,
  String? toFile,
  StreamSink<Food>? toStream,
  int sampleRate = 16000,
  int numChannels = 1,
  int bitRate = 16000,
  int bufferSize = 8192,
  bool enableVoiceProcessing = false,
  AudioSource audioSource = AudioSource.defaultSource,
}) async {
  _logger.d('FS:---> startRecorder ');
  await _lock.synchronized(() async {
    await _startRecorder(
      codec: codec,
      toFile: toFile,
      toStream: toStream,
      sampleRate: sampleRate,
      numChannels: numChannels,
      bitRate: bitRate,
      bufferSize: bufferSize,
      enableVoiceProcessing: enableVoiceProcessing,
      audioSource: audioSource,
    );
  });
  _logger.d('FS:<--- startRecorder ');
}