Home > Software design >  Flutter: PlatformException (PlatformException(, cannot find the file, null, null)) on FlutterAudioRe
Flutter: PlatformException (PlatformException(, cannot find the file, null, null)) on FlutterAudioRe

Time:08-16

class AudioRecorder {
  static DateTime now = DateTime.now();
  static String timestamp = now.toString();
  var recorder = FlutterAudioRecorder2("./file_path",
      audioFormat: AudioFormat.AAC); 
  // or var recorder = FlutterAudioRecorder2("file_path.mp3");
  startRecording() async {
    await recorder.initialized;
    await recorder.start(); // <- error here
  }

  stopRecording() async {
    var result = await recorder.stop();
  }
}

I can't find a way to fix this error, mainly because I don't understand what does it means, why it says "cannot find the file" but the file needs to be created? (since it's a recording?)

CodePudding user response:

Few points I noticed are

  1. Please check if its initialised or not in the initstate
await recorder.initialized;
//after initialisation add this
var current = await recorder.current(channel: 0);
//you should get the current status as initialised if its done properly.
  1. then on start recording use
await recorder.start()

Also make sure that the file path is right

Edit This is the code to init the recorder

_init() async {
    try {
      bool hasPermission = await FlutterAudioRecorder2.hasPermissions ?? false;

      if (hasPermission) {
        String customPath = '/flutter_audio_recorder_';
        io.Directory appDocDirectory;
//        io.Directory appDocDirectory = await getApplicationDocumentsDirectory();
        if (io.Platform.isIOS) {
          appDocDirectory = await getApplicationDocumentsDirectory();
        } else {
          appDocDirectory = (await getExternalStorageDirectory())!;
        }

        // can add extension like ".mp4" ".wav" ".m4a" ".aac"
        customPath = appDocDirectory.path   customPath   DateTime.now().millisecondsSinceEpoch.toString();

        _recorder = FlutterAudioRecorder2(customPath, audioFormat: AudioFormat.AAC);

        await _recorder!.initialized;
        // after initialization
        var current = await _recorder!.current(channel: 0);
        print(current);
        // should be "Initialized", if all working fine
      } else {
        ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(content: Text("You must accept permissions")));
      }
    } catch (e) {
      print(e);
    }
  }
  • Related