I am downloading audio file in mp3 and trying to play it with a music player but can't find the mp3 file anywhere. I can only see it from my app. No music player apps can access it. Here is the code.
Future<String?> saveAudio(bytes, fileName) async {
Directory directory;
try {
directory = (await getExternalStorageDirectory())!;
if (await directory.exists()) {
final saveFile = File('${directory.path}/$fileName.mp3');
await saveFile.writeAsBytes(bytes);
return saveFile.absolute.path;
}
} catch (e) {
ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(content: Text("An error occurred")));
}
return null;
}
downloadFile(bytes, fileName) async {
String? downloadedPath = await saveAudio(bytes, fileName);
if (downloadedPath == null) {
ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(content: Text("An error occurred")));
} else {
if (downloadedPath.isNotEmpty) {
AndroidIntent intent = AndroidIntent(
action: 'action_view',
data: Uri.encodeFull(downloadedPath),
type: "audio/*");
await intent.launch();
} else {
ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(content: Text("An error occurred")));
}
}
}