Reputation: 982
I am using the just_audio
package to handle audio playback in my Flutter app. Currently, when a corrupted audio file is encountered, I get an error (for example, "File not found" or a PlayerException
). I want to handle these errors gracefully and notify the user that the file is corrupted, along with options like retrying or reporting the issue.
Here’s my code for playing audio from a file:
Future<void> playFromFile(List<int> bytes, {String? songPath = ""}) async {
try {
File file = File(songPath?? "");
bool pathExists = await file.exists();
if (pathExists) {
_playlist.add(AudioSource.file(songPath ?? ""));
await _player.play();
} else {
DebugLog.e('playFromFile File not found');
}
} on PlayerException catch (e) {
DebugLog.e("playFromFile Error code: ${e.code}");
DebugLog.e("playFromFile Error message: ${e.message}");
} on PlayerInterruptedException catch (e) {
DebugLog.e("playFromFile Connection aborted: ${e.message}");
}
catch (e) {
SentryService.instance.captureException(e);
DebugLog.e('playFromFile Error playing file audio: $e');
}
}
The problem:
When the file is corrupted or unable to play, I want to handle it appropriately. I tried to catch the error within the catch
block, but I am unsure whether this is the best approach.
playFromFile
, or in another place)?Any insights or code suggestions would be greatly appreciated!
Upvotes: -1
Views: 23