Plugin-specific exceptions

This commit is contained in:
Ryan Heise 2020-08-07 22:11:28 +10:00
parent 1e3716e722
commit 0524d4302b
3 changed files with 42 additions and 9 deletions

View File

@ -820,7 +820,7 @@
} }
- (void)sendErrorForItem:(IndexedPlayerItem *)playerItem { - (void)sendErrorForItem:(IndexedPlayerItem *)playerItem {
FlutterError *flutterError = [FlutterError errorWithCode:[NSString stringWithFormat:@"%d", playerItem.error] FlutterError *flutterError = [FlutterError errorWithCode:[NSString stringWithFormat:@"%d", playerItem.error.code]
message:playerItem.error.localizedDescription message:playerItem.error.localizedDescription
details:nil]; details:nil];
[self sendError:flutterError playerItem:playerItem]; [self sendError:flutterError playerItem:playerItem];

View File

@ -70,8 +70,8 @@ class _MyAppState extends State<MyApp> {
try { try {
await _player.load(_playlist); await _player.load(_playlist);
} catch (e) { } catch (e) {
// catch load errors: 404 url, wrong url ... // catch load errors: 404, invalid url ...
print("$e"); print("An error occured (${e.code}): ${e.message}");
} }
} }

View File

@ -364,11 +364,13 @@ class AudioPlayer {
load(AudioSource.uri(Uri.parse('asset://$assetPath'))); load(AudioSource.uri(Uri.parse('asset://$assetPath')));
/// Loads audio from an [AudioSource] and completes when the audio is ready /// Loads audio from an [AudioSource] and completes when the audio is ready
/// to play with the duration of that audio, or an exception if this call was /// to play with the duration of that audio, or null if the duration is unknown.
/// interrupted by another call to [load], or if for any reason the audio
/// source was unable to be loaded.
/// ///
/// If the duration is unknown, null will be returned. /// This method throws:
///
/// * [PlayerException] if the audio source was unable to be loaded.
/// * [PlayerInterruptedException] if another call to [load] happened before
/// this call completed.
Future<Duration> load(AudioSource source) async { Future<Duration> load(AudioSource source) async {
try { try {
_audioSource = source; _audioSource = source;
@ -403,8 +405,15 @@ class AudioPlayer {
_durationSubject.add(duration); _durationSubject.add(duration);
return duration; return duration;
} on PlatformException catch (e) { } on PlatformException catch (e) {
// TODO: Create own exception type. try {
throw Exception(e.message); throw PlayerException(int.parse(e.code), e.message);
} on FormatException catch (_) {
if (e.code == 'abort') {
throw PlayerInterruptedException(e.message);
} else {
throw PlayerException(9999999, e.message);
}
}
} }
} }
@ -571,6 +580,30 @@ class AudioPlayer {
(await _channel).invokeMethod(method, args); (await _channel).invokeMethod(method, args);
} }
/// Captures the details of any error accessing, loading or playing an audio
/// source, including an invalid or inaccessible URL, or an audio encoding that
/// could not be understood.
class PlayerException {
/// On iOS and macOS, maps to `NSError.code`. On Android, maps to
/// `ExoPlaybackException.type`. On Web, maps to `MediaError.code`.
final int code;
/// On iOS and macOS, maps to `NSError.localizedDescription`. On Android,
/// maps to `ExoPlaybackException.getMessage()`. On Web, a generic message
/// is provided.
final String message;
PlayerException(this.code, this.message);
}
/// An error that occurs when one operation on the player has been interrupted
/// (e.g. by another simultaneous operation).
class PlayerInterruptedException {
final String message;
PlayerInterruptedException(this.message);
}
/// Encapsulates the playback state and current position of the player. /// Encapsulates the playback state and current position of the player.
class PlaybackEvent { class PlaybackEvent {
/// The current processing state. /// The current processing state.