我正在为iOS创建一个流媒体广播应用程序,希望通过调整AVPlayer和AVPlayerItem的属性,在不稳定的网络连接条件下获得更可靠的播放效果。
我想增加缓冲区大小。
我找到的唯一答案是这里。
有没有不需要使用OpenAL就能实现这一点的方法?
我找到的唯一答案是这里。
有没有不需要使用OpenAL就能实现这一点的方法?
NSArray *timeRanges = (NSArray *)[change objectForKey:NSKeyValueChangeNewKey];
CMTimeRange timerange = [timeRanges[0] CMTimeRangeValue];
CGFloat smartValue = CMTimeGetSeconds(CMTimeAdd(timerange.start, timerange.duration));
CGFloat duration = CMTimeGetSeconds(self.player.currentTime);
if(smartValue - duration > 5 || smartValue == duration) {
// Change the value "5" to your needed secs, its the buffer size.
// Play the video
}
我已经实现了并且工作得很好。
#define VIDEO_BUFFER_READY_PERCENT 0.3
- (void)viewDidLoad{
[super viewDidLoad];
[self.player addObserver:self forKeyPath:@"currentItem.loadedTimeRanges" options:NSKeyValueObservingOptionNew context:&kTimeRangesKVO];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (context == &kTimeRangesKVO) {
float percent = CMTimeGetSeconds(timerange.duration) / CMTimeGetSeconds(self.player.currentItem.duration);
if (percent > VIDEO_BUFFER_READY_PERCENT) {
NSLog(@" . . . %.5f -> %.5f, %f percent", CMTimeGetSeconds(timerange.duration), CMTimeGetSeconds(CMTimeAdd(timerange.start, timerange.duration)), percent);
[self.player prerollAtRate:0.0 completionHandler:^(BOOL finished) {
[self.player seekToTime:kCMTimeZero];
}
}
else{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}