Better error handling on iOS

This commit is contained in:
Ryan Heise 2020-08-01 03:20:36 +10:00
parent ebc8584b97
commit 37fe2c865e
2 changed files with 36 additions and 36 deletions

View File

@ -141,11 +141,6 @@
} else {
result(FlutterMethodNotImplemented);
}
// TODO
/* } catch (Exception e) { */
/* e.printStackTrace(); */
/* result.error("Error", null, null); */
/* } */
}
// Untested
@ -467,7 +462,14 @@
}
- (void)load:(NSDictionary *)source result:(FlutterResult)result {
// TODO: error if already connecting
if (_state == connecting) {
[self abortExistingConnection];
_playing = NO;
[_player pause];
} else if (_state == playing) {
_playing = NO;
[_player pause];
}
_connectionResult = result;
_index = 0;
[self updatePosition];
@ -671,13 +673,7 @@
}
case AVPlayerItemStatusFailed: {
NSLog(@"AVPlayerItemStatusFailed");
if (playerItem != _player.currentItem) return;
if (_connectionResult) {
_connectionResult([FlutterError errorWithCode:[NSString stringWithFormat:@"%d", _player.currentItem.error]
message:_player.currentItem.error.localizedDescription
details:nil]);
_connectionResult = nil;
}
[self sendErrorForItem:playerItem];
break;
}
case AVPlayerItemStatusUnknown:
@ -777,6 +773,31 @@
}
}
- (void)sendErrorForItem:(IndexedPlayerItem *)playerItem {
FlutterError *flutterError = [FlutterError errorWithCode:[NSString stringWithFormat:@"%d", playerItem.error]
message:playerItem.error.localizedDescription
details:nil];
[self sendError:flutterError playerItem:playerItem];
}
- (void)sendError:(FlutterError *)flutterError playerItem:(IndexedPlayerItem *)playerItem {
if (_connectionResult && playerItem == _player.currentItem) {
_connectionResult(flutterError);
_connectionResult = nil;
}
if (_eventSink) {
// Broadcast all errors even if they aren't on the current item.
_eventSink(flutterError);
}
}
- (void)abortExistingConnection {
FlutterError *flutterError = [FlutterError errorWithCode:@"abort"
message:@"Connection aborted"
details:nil];
[self sendError:flutterError playerItem:nil];
}
- (int)indexForItem:(IndexedPlayerItem *)playerItem {
for (int i = 0; i < _indexedAudioSources.count; i++) {
if (_indexedAudioSources[i].playerItem == playerItem) {

View File

@ -29,33 +29,12 @@ class _MyAppState extends State<MyApp> {
),
),
),
//LoopingAudioSource(
// count: 2,
// child: AudioSource.uri(
// Uri.parse(
// "https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3"),
// tag: AudioMetadata(
// album: "Science Friday",
// title: "A Salute To Head-Scratching Science (full)",
// ),
// ),
//),
//ClippingAudioSource(
// start: Duration(seconds: 60),
// end: Duration(seconds: 65),
// child: AudioSource.uri(Uri.parse(
// "https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3")),
// tag: AudioMetadata(
// album: "Science Friday",
// title: "A Salute To Head-Scratching Science (5 seconds)",
// ),
//),
AudioSource.uri(
Uri.parse(
"https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3"),
tag: AudioMetadata(
album: "Science Friday",
title: "A Salute To Head-Scratching Science (full)",
title: "A Salute To Head-Scratching Science",
),
),
AudioSource.uri(
@ -84,7 +63,7 @@ class _MyAppState extends State<MyApp> {
try {
await _player.load(_playlist);
} catch (e) {
// catch audio error ex: 404 url, wrong url ...
// catch load errors: 404 url, wrong url ...
print("$e");
}
}