Reputation: 153
I'm trying to record audios in my app but I'm getting this error
Caused by: com.google.android.exoplayer2.upstream.FileDataSource$FileDataSourceException: java.io.FileNotFoundException: /data/user/0/com.myapp.example/app_flutter/1655521208092.wav: open failed: ENOENT (No such file or directory)
I'm using path provider and this directory really doesnt exist, how can I create this directory or use a directory that already exists? Like, detect one already created directory or create one and then record the audio inside this directory?
Directory directory = await getApplicationDocumentsDirectory();
filepath = directory.path + '/' + DateTime.now().millisecondsSinceEpoch.toString() + '.wav';
_myRecorder = FlutterSoundRecorder();
Upvotes: 0
Views: 1900
Reputation: 937
Use it like this. If directory exists then get it otherwise create a new directory.
Future<void> startRecording() async {
Directory appDir = await getExternalStorageDirectory();
String jrecord = 'Audiorecords';
String dato = "${DateTime.now().millisecondsSinceEpoch?.toString()}.wav";
Directory appDirec =
Directory("${appDir.path}/$jrecord/");
if (await appDirec.exists()) {
playAudio.value = true;
String patho = "${appDirec.path}$dato";
print("path for file11 ${patho}");
_recordingSession.openAudioSession();
await _recordingSession.startRecorder(
toFile: patho,
codec: Codec.pcm16WAV,
);
_recordingSession.onProgress.listen((e) {
var date = DateTime.fromMillisecondsSinceEpoch(e.duration.inMilliseconds,
isUtc: true);
var timeText = DateFormat('mm:ss:SS', 'en_GB').format(date);
timerText.value = timeText.substring(0, 8);
});
} else {
appDirec.create(recursive: true);
String patho = "${appDirec.path}$dato";
print("path for file22 ${patho}");
_recordingSession.openAudioSession();
await _recordingSession.startRecorder(
toFile: patho,
codec: Codec.pcm16WAV,
);
_recordingSession.onProgress.listen((e) {
var date = DateTime.fromMillisecondsSinceEpoch(e.duration.inMilliseconds,
isUtc: true);
var timeText = DateFormat('mm:ss:SS', 'en_GB').format(date);
timerText.value = timeText.substring(0, 8);
});
}
}
Upvotes: 2