Queue seek request until ready to play on iOS.

This commit is contained in:
Ryan Heise 2020-10-15 13:51:41 +11:00
parent 5d0ce955f4
commit 037795674f
6 changed files with 27 additions and 0 deletions

View File

@ -672,6 +672,7 @@
if ([statusNumber isKindOfClass:[NSNumber class]]) {
status = statusNumber.intValue;
}
[playerItem.audioSource onStatusChanged:status];
switch (status) {
case AVPlayerItemStatusReadyToPlay: {
if (playerItem != _player.currentItem) return;

View File

@ -56,6 +56,8 @@
if (!completionHandler || (self.playerItem.status == AVPlayerItemStatusReadyToPlay)) {
CMTime absPosition = CMTimeAdd(_start, position);
[_audioSource.playerItem seekToTime:absPosition toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero completionHandler:completionHandler];
} else {
[super seek:position completionHandler:completionHandler];
}
}

View File

@ -4,15 +4,31 @@
@implementation IndexedAudioSource {
BOOL _isAttached;
CMTime _queuedSeekPos;
void (^_queuedSeekCompletionHandler)(BOOL);
}
- (instancetype)initWithId:(NSString *)sid {
self = [super init];
NSAssert(self, @"super init cannot be nil");
_isAttached = NO;
_queuedSeekPos = kCMTimeInvalid;
_queuedSeekCompletionHandler = nil;
return self;
}
- (void)onStatusChanged:(AVPlayerItemStatus)status {
if (status == AVPlayerItemStatusReadyToPlay) {
// This handles a pending seek during a load.
// TODO: Test seeking during a seek.
if (_queuedSeekCompletionHandler) {
[self seek:_queuedSeekPos completionHandler:_queuedSeekCompletionHandler];
_queuedSeekPos = kCMTimeInvalid;
_queuedSeekCompletionHandler = nil;
}
}
}
- (IndexedPlayerItem *)playerItem {
return nil;
}
@ -48,6 +64,10 @@
}
- (void)seek:(CMTime)position completionHandler:(void (^)(BOOL))completionHandler {
if (completionHandler && (self.playerItem.status != AVPlayerItemStatusReadyToPlay)) {
_queuedSeekPos = position;
_queuedSeekCompletionHandler = completionHandler;
}
}
- (CMTime)duration {

View File

@ -56,6 +56,8 @@
position = CMTimeAdd(position, range.start);
}
[_playerItem seekToTime:position toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero completionHandler:completionHandler];
} else {
[super seek:position completionHandler:completionHandler];
}
}

View File

@ -11,6 +11,7 @@
@property (readonly, nonatomic) CMTime bufferedPosition;
@property (readonly, nonatomic) BOOL isAttached;
- (void)onStatusChanged:(AVPlayerItemStatus)status;
- (void)attach:(AVQueuePlayer *)player;
- (void)play:(AVQueuePlayer *)player;
- (void)pause:(AVQueuePlayer *)player;

View File

@ -11,6 +11,7 @@
@property (readonly, nonatomic) CMTime bufferedPosition;
@property (readonly, nonatomic) BOOL isAttached;
- (void)onStatusChanged:(AVPlayerItemStatus)status;
- (void)attach:(AVQueuePlayer *)player;
- (void)play:(AVQueuePlayer *)player;
- (void)pause:(AVQueuePlayer *)player;