2014-01-28 44 views
8

我正在爲iOS創建流式無線電應用程序,我想調整AVPlayer和AVPlayerItem的屬性,以便在有損連接條件下爲我提供更可靠的播放。 我想增加緩衝區大小。AVPlayer/AVPlayerItem的緩衝區大小

我能找到的唯一答案是here

反正有沒有做到這一點,而不去OpenAL的?

+0

請按照這個答案。 http://stackoverflow.com/questions/4218090/pre-buffering-for-avqueueplayer/39036307#39036307可能對你有所幫助。 – iPatel

回答

10

在您的觀察者方法中添加下面的一段代碼。

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 
} 

我已經實施並且工作良好。

推薦人:https://stackoverflow.com/a/7730708/2315453

+1

我不明白如何改變「5」會改變玩家實際緩衝的程度?請解釋一下 – dev

+1

你能解釋一下嗎?你正在更新哪個值? – DivineDesert

+0

http://stackoverflow.com/questions/4495433/uislider-with-progressview-combined這可能會有所幫助。 – arunit21

1

在這裏看到:AVPlayer streaming progress

在這裏:How to get file size and current file size from NSURL for AVPlayer iOS4.0

你可以觀察玩家的財產「currentitem.loadedTimeRanges」,而當事件被拋出,你可以檢查有多少緩衝,前開始播放。以下是我如何使用它的一個示例:

#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]; 
    } 
+9

你的回答告訴我如何觀察/檢索緩衝區,但它不告訴我如何增加緩衝區大小。 – Ospho