createRecorderBackend function

Future<AudioRecorderBackend> createRecorderBackend({
  1. int sampleRate = 48000,
  2. Duration maxDuration = const Duration(minutes: 10),
})

Returns a RecordAudioRecorderBackend when the native plugin responds, otherwise MockAudioRecorderBackend (simulator / stale binary / desktop).

Call once at engine construction — probes hasPermission to detect MissingPluginException before the user taps Record.

Implementation

Future<AudioRecorderBackend> createRecorderBackend({
  int sampleRate = 48000,
  Duration maxDuration = const Duration(minutes: 10),
}) async {
  try {
    final probe = AudioRecorder();
    await probe.hasPermission();
    await probe.dispose();
    return RecordAudioRecorderBackend(
      sampleRate: sampleRate,
      maxDuration: maxDuration,
    );
  } on MissingPluginException catch (e) {
    debugPrint(
      'createRecorderBackend: record plugin missing ($e) — using mock capture.',
    );
    return MockAudioRecorderBackend();
  } catch (e) {
    // Permission denied / other — still use the real backend so the OS
    // permission prompt can surface on the next start().
    debugPrint('createRecorderBackend: probe note: $e');
    return RecordAudioRecorderBackend(
      sampleRate: sampleRate,
      maxDuration: maxDuration,
    );
  }
}