From 37fe2c865ecf32401a7ecb95af12d7f3a36a04e3 Mon Sep 17 00:00:00 2001 From: Ryan Heise Date: Sat, 1 Aug 2020 03:20:36 +1000 Subject: [PATCH] Better error handling on iOS --- darwin/Classes/AudioPlayer.m | 47 ++++++++++++++++++++++++++---------- example/lib/main.dart | 25 ++----------------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/darwin/Classes/AudioPlayer.m b/darwin/Classes/AudioPlayer.m index fca125d..1832f65 100644 --- a/darwin/Classes/AudioPlayer.m +++ b/darwin/Classes/AudioPlayer.m @@ -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) { diff --git a/example/lib/main.dart b/example/lib/main.dart index 489d745..3fffdbc 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -29,33 +29,12 @@ class _MyAppState extends State { ), ), ), - //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 { try { await _player.load(_playlist); } catch (e) { - // catch audio error ex: 404 url, wrong url ... + // catch load errors: 404 url, wrong url ... print("$e"); } }