'Closed: How to read a temporary file as bytes in Flutter?

So basically I am using flutter_sound to record an audio file:

class AudioRecorder {
  FlutterSoundRecorder? _audioRecorder;
  String tempFilePath = "audio_recording.mp4";

  Future<void> init() async {
    _audioRecorder = FlutterSoundRecorder();
    await _audioRecorder!.openRecorder();
  }

  Future<void> dispose() async {
    await _audioRecorder!.openRecorder();
    _audioRecorder = null;
  }

  Future<void> startRecording() async {
    await _audioRecorder!.startRecorder(toFile: tempFilePath);
  }

  Future<File> stopRecording() async {
    await _audioRecorder!.stopRecorder();
    return File(tempFilePath);
  }
}

This class works as intended and a file is returned by stopRecording();.

The issue:

When I try to audioFile.readAsBytesSync();, this error occurs:

Unhandled Exception: FileSystemException: Cannot open file, path = 'audio_recording.mp4' (OS Error: No such file or directory, errno = 2)

What I think is wrong:

When I do print(audioFile.path);, the output is "audio_recording.mp4" however when I stop recording the audio, flutter_sound also prints a path to the file but its different:

"/Users/.../Library/Developer/CoreSimulator/Devices/.../data/Containers/Data/Application/.../tmp/audio_recording.mp4"

When I readAsBytesSync to a File object that has this path, there is no exception. So I somehow have to get this path but I cannot figure out how. I have tried using path_provider and getTemporaryDirectory but its to no avail since the path from getTemporaryDirectory is different to the one printed by flutter_sound.

How could I fix this?

Thank you!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source