Broadcast duration events and setUrl errors on iOS

This commit is contained in:
Ryan Heise 2020-06-09 03:29:36 +10:00
parent 841bd10822
commit fa1bd5654b
1 changed files with 22 additions and 18 deletions

View File

@ -12,7 +12,7 @@
enum PlaybackState _state; enum PlaybackState _state;
long long _updateTime; long long _updateTime;
int _updatePosition; int _updatePosition;
int _seekPos; CMTime _seekPos;
FlutterResult _connectionResult; FlutterResult _connectionResult;
BOOL _buffering; BOOL _buffering;
id _endObserver; id _endObserver;
@ -36,7 +36,7 @@
[_eventChannel setStreamHandler:self]; [_eventChannel setStreamHandler:self];
_state = none; _state = none;
_player = nil; _player = nil;
_seekPos = -1; _seekPos = kCMTimeInvalid;
_buffering = NO; _buffering = NO;
_endObserver = 0; _endObserver = 0;
_timeObserver = 0; _timeObserver = 0;
@ -74,7 +74,7 @@
[self setAutomaticallyWaitsToMinimizeStalling:(BOOL)[args[0] boolValue]]; [self setAutomaticallyWaitsToMinimizeStalling:(BOOL)[args[0] boolValue]];
result(nil); result(nil);
} else if ([@"seek" isEqualToString:call.method]) { } else if ([@"seek" isEqualToString:call.method]) {
int position = args[0] == [NSNull null] ? -2 : [args[0] intValue]; CMTime position = args[0] == [NSNull null] ? kCMTimePositiveInfinity : CMTimeMake([args[0] intValue], 1000);
[self seek:position result:result]; [self seek:position result:result];
result(nil); result(nil);
} else if ([@"dispose" isEqualToString:call.method]) { } else if ([@"dispose" isEqualToString:call.method]) {
@ -135,19 +135,28 @@
@(_updatePosition), @(_updatePosition),
// TODO: Icy Metadata // TODO: Icy Metadata
[NSNull null], [NSNull null],
@([self getDuration]),
]); ]);
} }
- (int)getCurrentPosition { - (int)getCurrentPosition {
if (_state == none || _state == connecting) { if (_state == none || _state == connecting) {
return 0; return 0;
} else if (_seekPos >= 0) { } else if (CMTIME_IS_VALID(_seekPos)) {
return _seekPos; return (int)(1000 * CMTimeGetSeconds(_seekPos));
} else { } else {
return (int)(1000 * CMTimeGetSeconds([_player currentTime])); return (int)(1000 * CMTimeGetSeconds([_player currentTime]));
} }
} }
- (int)getDuration {
if (_state == none || _state == connecting) {
return -1;
} else {
return (int)(1000 * CMTimeGetSeconds([[_player currentItem] duration]));
}
}
- (void)setPlaybackState:(enum PlaybackState)state { - (void)setPlaybackState:(enum PlaybackState)state {
//enum PlaybackState oldState = _state; //enum PlaybackState oldState = _state;
_state = state; _state = state;
@ -247,11 +256,14 @@
switch (status) { switch (status) {
case AVPlayerItemStatusReadyToPlay: case AVPlayerItemStatusReadyToPlay:
[self setPlaybackState:stopped]; [self setPlaybackState:stopped];
_connectionResult(@((int)(1000 * CMTimeGetSeconds([[_player currentItem] duration])))); _connectionResult(@([self getDuration]));
break; break;
case AVPlayerItemStatusFailed: case AVPlayerItemStatusFailed:
NSLog(@"AVPlayerItemStatusFailed"); NSLog(@"AVPlayerItemStatusFailed");
_connectionResult(nil); _connectionResult([FlutterError errorWithCode:[NSString stringWithFormat:@"%d", _player.currentItem.error]
message:_player.currentItem.error.localizedDescription
details:nil
]);
break; break;
case AVPlayerItemStatusUnknown: case AVPlayerItemStatusUnknown:
break; break;
@ -356,28 +368,20 @@
} }
} }
- (void)seek:(int)position result:(FlutterResult)result { - (void)seek:(CMTime)position result:(FlutterResult)result {
_seekPos = position; _seekPos = position;
NSLog(@"seek. enter buffering"); NSLog(@"seek. enter buffering");
_buffering = YES; _buffering = YES;
[self broadcastPlaybackEvent]; [self broadcastPlaybackEvent];
if (position == -2) { [_player seekToTime:position
[_player seekToTime:kCMTimePositiveInfinity
completionHandler:^(BOOL finished) { completionHandler:^(BOOL finished) {
NSLog(@"seek completed"); NSLog(@"seek completed");
[self onSeekCompletion:result]; [self onSeekCompletion:result];
}]; }];
} else {
[_player seekToTime:CMTimeMake(position, 1000)
completionHandler:^(BOOL finished) {
NSLog(@"seek completed");
[self onSeekCompletion:result];
}];
}
} }
- (void)onSeekCompletion:(FlutterResult)result { - (void)onSeekCompletion:(FlutterResult)result {
_seekPos = -1; _seekPos = kCMTimeInvalid;
_buffering = NO; _buffering = NO;
[self broadcastPlaybackEvent]; [self broadcastPlaybackEvent];
result(nil); result(nil);