Skip to main content Link Menu Expand (external link) Document Search Copy Copied
startRecorder method - FlutterSoundRecorder class - recorder library - Dart API
menu
startRecorder

startRecorder method

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

startRecorder() starts recording a recorder.


If an openRecorder() is in progress, startRecorder() will automatically wait the end of the opening.

Parameters

  • codec: The codec to be used. Please refer to the Codec compatibility Table to know which codecs are currently supported.
  • One of the following parameters
    • 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 interleaved. Please look to the following notice.
    • toStreamFloat32: if you want to record to a Dart Stream NOT interleaved with a Float32 coding. Please look to the following notice.
    • toStreamInt16: if you want to record to a Dart Stream NOT interleaved with a Int16 coding. Please look to the following notice.
  • sampleRate: The sample rate in Hertz (used only with PCM codecs)
  • numChannels: The number of channels (1=monophony, 2=stereophony) (used only with PCM codecs)
  • bitRate: The bit rate in Hertz. Optional
  • audioSource : possible value is :
    • defaultSource
    • microphone
    • voiceDownlink (if someone can explain me what it is, I will be grateful ;-) )

Hint: 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'

See also


Implementation

Future<void> startRecorder({
  Codec codec = Codec.defaultCodec,
  String? toFile,
  StreamSink<List<Float32List>>? toStreamFloat32,
  StreamSink<List<Int16List>>? toStreamInt16,
  StreamSink<Uint8List>? toStream,
  int? sampleRate,
  int numChannels = 1,
  int bitRate = 16000,
  int bufferSize = 8192,
  bool enableVoiceProcessing = false,
  AudioSource audioSource = AudioSource.defaultSource,
}) async {
  _logger.d('FS:---> startRecorder ');

  if ((codec == Codec.pcm16 || codec == Codec.pcmFloat32) &&
      (toStream != null ||
          toStreamFloat32 != null ||
          toStreamInt16 != null) &&
      (!kIsWeb) &&
      Platform
          .isIOS) // This hack is just to have recorder to stream working correctly.
  {
    FlutterSoundRecorder recorder = FlutterSoundRecorder();
    await recorder.openRecorder();
    try {
      await recorder.startRecorder(
        codec: Codec.aacMP4,
        toFile: "FlutterSound.mp4",
        audioSource: audioSource,
      );
    } catch (e) {
      _logger.d('Hacking the bug we have on iOS when recording to stream');
    }
    await recorder.stopRecorder();
    await recorder.closeRecorder();
  }

  await stopRecorder(); // No two recorders at the same time
  if (sampleRate == null) {
    sampleRate = await getSampleRate();
    if (sampleRate == 0) {
      sampleRate = 16000;
    }
  }

  await _lock.synchronized(() async {
    await _startRecorder(
      codec: codec,
      toFile: toFile,
      toStream: toStream,
      toStreamFloat32: toStreamFloat32,
      toStreamInt16: toStreamInt16,
      //timeSlice: timeSlice,
      sampleRate: sampleRate!,
      numChannels: numChannels,
      bitRate: bitRate,
      bufferSize: bufferSize,
      enableVoiceProcessing: enableVoiceProcessing,
      audioSource: audioSource,
    );
  });
  _logger.d('FS:<--- startRecorder ');
}
flutter_sound 9.25.3