Implement bufferedPosition on iOS

This commit is contained in:
Ryan Heise 2020-08-01 14:59:22 +10:00
parent 2596da4a5b
commit 6d14d257a0
6 changed files with 46 additions and 0 deletions

View File

@ -28,6 +28,7 @@
long long _updateTime; long long _updateTime;
int _updatePosition; int _updatePosition;
int _lastPosition; int _lastPosition;
int _bufferedPosition;
// Set when the current item hasn't been played yet so we aren't sure whether sufficient audio has been buffered. // Set when the current item hasn't been played yet so we aren't sure whether sufficient audio has been buffered.
BOOL _bufferUnconfirmed; BOOL _bufferUnconfirmed;
CMTime _seekPos; CMTime _seekPos;
@ -67,6 +68,7 @@
_updatePosition = 0; _updatePosition = 0;
_updateTime = 0; _updateTime = 0;
_lastPosition = 0; _lastPosition = 0;
_bufferedPosition = 0;
_bufferUnconfirmed = NO; _bufferUnconfirmed = NO;
_playing = NO; _playing = NO;
_automaticallyWaitsToMinimizeStalling = YES; _automaticallyWaitsToMinimizeStalling = YES;
@ -330,6 +332,18 @@
} }
} }
- (int)getBufferedPosition {
if (_state == none || _state == connecting) {
return 0;
} else if (_indexedAudioSources) {
int ms = (int)(1000 * CMTimeGetSeconds(_indexedAudioSources[_index].bufferedPosition));
if (ms < 0) ms = 0;
return ms;
} else {
return 0;
}
}
- (int)getDuration { - (int)getDuration {
if (_state == none) { if (_state == none) {
return -1; return -1;
@ -368,6 +382,7 @@
// Get notified of the buffer state // Get notified of the buffer state
[playerItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil]; [playerItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil];
[playerItem addObserver:self forKeyPath:@"playbackBufferFull" options:NSKeyValueObservingOptionNew context:nil]; [playerItem addObserver:self forKeyPath:@"playbackBufferFull" options:NSKeyValueObservingOptionNew context:nil];
[playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
//[playerItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil]; //[playerItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];
// Get notified when playback has reached the end // Get notified when playback has reached the end
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onComplete:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onComplete:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
@ -781,6 +796,14 @@
// Already at zero, no need to seek. // Already at zero, no need to seek.
} }
} }
} else if ([keyPath isEqualToString:@"loadedTimeRanges"]) {
IndexedPlayerItem *playerItem = (IndexedPlayerItem *)object;
if (playerItem != _player.currentItem) return;
int pos = [self getBufferedPosition];
if (pos != _bufferedPosition) {
_bufferedPosition = pos;
[self broadcastPlaybackEvent];
}
} }
} }

View File

@ -68,4 +68,10 @@
return CMTimeSubtract(self.playerItem.currentTime, _start); return CMTimeSubtract(self.playerItem.currentTime, _start);
} }
- (CMTime)bufferedPosition {
CMTime pos = CMTimeSubtract(_audioSource.bufferedPosition, _start);
CMTime dur = [self duration];
return CMTimeCompare(pos, dur) >= 0 ? dur : pos;
}
@end @end

View File

@ -61,4 +61,8 @@
return kCMTimeInvalid; return kCMTimeInvalid;
} }
- (CMTime)bufferedPosition {
return kCMTimeInvalid;
}
@end @end

View File

@ -63,4 +63,15 @@
return _playerItem.currentTime; return _playerItem.currentTime;
} }
- (CMTime)bufferedPosition {
NSValue *last = _playerItem.loadedTimeRanges.lastObject;
if (last) {
CMTimeRange timeRange = [last CMTimeRangeValue];
return CMTimeAdd(timeRange.start, timeRange.duration);
} else {
return _playerItem.currentTime;
}
return kCMTimeInvalid;
}
@end @end

View File

@ -8,6 +8,7 @@
@property (readonly, nonatomic) IndexedPlayerItem *playerItem; @property (readonly, nonatomic) IndexedPlayerItem *playerItem;
@property (readwrite, nonatomic) CMTime duration; @property (readwrite, nonatomic) CMTime duration;
@property (readonly, nonatomic) CMTime position; @property (readonly, nonatomic) CMTime position;
@property (readonly, nonatomic) CMTime bufferedPosition;
@property (readonly, nonatomic) BOOL isAttached; @property (readonly, nonatomic) BOOL isAttached;
- (void)attach:(AVQueuePlayer *)player; - (void)attach:(AVQueuePlayer *)player;

View File

@ -8,6 +8,7 @@
@property (readonly, nonatomic) IndexedPlayerItem *playerItem; @property (readonly, nonatomic) IndexedPlayerItem *playerItem;
@property (readwrite, nonatomic) CMTime duration; @property (readwrite, nonatomic) CMTime duration;
@property (readonly, nonatomic) CMTime position; @property (readonly, nonatomic) CMTime position;
@property (readonly, nonatomic) CMTime bufferedPosition;
@property (readonly, nonatomic) BOOL isAttached; @property (readonly, nonatomic) BOOL isAttached;
- (void)attach:(AVQueuePlayer *)player; - (void)attach:(AVQueuePlayer *)player;