Fix staling on start on iOS (#60)

* Fix warning

* Fix Staling on Start
This commit is contained in:
Mohamed Abadllah 2020-03-25 15:16:51 +02:00 committed by GitHub
parent 3d3805145a
commit 05504374ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 4 deletions

View File

@ -165,6 +165,7 @@
[self setPlaybackState:connecting]; [self setPlaybackState:connecting];
if (_player) { if (_player) {
[[_player currentItem] removeObserver:self forKeyPath:@"status"]; [[_player currentItem] removeObserver:self forKeyPath:@"status"];
if (@available(iOS 10.0, *)) {[_player removeObserver:self forKeyPath:@"timeControlStatus"];}
[[NSNotificationCenter defaultCenter] removeObserver:_endObserver]; [[NSNotificationCenter defaultCenter] removeObserver:_endObserver];
_endObserver = 0; _endObserver = 0;
} }
@ -203,6 +204,10 @@
} }
if (@available(iOS 10.0, *)) { if (@available(iOS 10.0, *)) {
_player.automaticallyWaitsToMinimizeStalling = _automaticallyWaitsToMinimizeStalling; _player.automaticallyWaitsToMinimizeStalling = _automaticallyWaitsToMinimizeStalling;
[_player addObserver:self
forKeyPath:@"timeControlStatus"
options:NSKeyValueObservingOptionNew
context:nil];
} }
// TODO: learn about the different ways to define weakSelf. // TODO: learn about the different ways to define weakSelf.
//__weak __typeof__(self) weakSelf = self; //__weak __typeof__(self) weakSelf = self;
@ -241,6 +246,27 @@
break; break;
} }
} }
if (@available(iOS 10.0, *)) {
if ([keyPath isEqualToString:@"timeControlStatus"]) {
AVPlayerTimeControlStatus status = AVPlayerTimeControlStatusPaused;
NSNumber *statusNumber = change[NSKeyValueChangeNewKey];
if ([statusNumber isKindOfClass:[NSNumber class]]) {
status = statusNumber.integerValue;
}
switch (status) {
case AVPlayerTimeControlStatusPaused:
[self setPlaybackBufferingState:paused buffering:NO];
break;
case AVPlayerTimeControlStatusWaitingToPlayAtSpecifiedRate:
if (_state != stopped) [self setPlaybackBufferingState:stopped buffering:YES];
else [self setPlaybackBufferingState:connecting buffering:YES];
break;
case AVPlayerTimeControlStatusPlaying:
[self setPlaybackBufferingState:playing buffering:NO];
break;
}
}
}
} }
- (void)setClip:(NSNumber*)start end:(NSNumber*)end { - (void)setClip:(NSNumber*)start end:(NSNumber*)end {
@ -252,7 +278,7 @@
//int lag = 6; //int lag = 6;
//int start = [self getCurrentPosition]; //int start = [self getCurrentPosition];
[_player play]; [_player play];
[self setPlaybackState:playing]; if (!@available(iOS 10.0, *)) {[self setPlaybackState:playing];}
// TODO: convert this Android code to iOS // TODO: convert this Android code to iOS
/* if (endDetector != null) { */ /* if (endDetector != null) { */
/* handler.removeCallbacks(endDetector); */ /* handler.removeCallbacks(endDetector); */
@ -276,7 +302,7 @@
- (void)pause { - (void)pause {
[_player pause]; [_player pause];
[self setPlaybackState:paused]; if (!@available(iOS 10.0, *)) {[self setPlaybackState:paused];}
} }
- (void)stop { - (void)stop {
@ -301,8 +327,8 @@
- (void)setSpeed:(float)speed { - (void)setSpeed:(float)speed {
if (speed == 1.0 if (speed == 1.0
|| speed < 1.0 && _player.currentItem.canPlaySlowForward || (speed < 1.0 && _player.currentItem.canPlaySlowForward)
|| speed > 1.0 && _player.currentItem.canPlayFastForward) { || (speed > 1.0 && _player.currentItem.canPlayFastForward)) {
_player.rate = speed; _player.rate = speed;
} }
} }