2017-04-26 8 views
1

AVPlayerItemがストリームの最後までバッファリングされているかどうかを判断する最善の方法を判断しようとしています。バッファがいっぱいであるだけでなく、追加のバッファリングなしで残りのアイテムを再生するのに必要なものがすべてバッファに格納されていること。 AVPlayerItemはisPlaybackBufferFull呼び出しを提供しますが、アイテムの再生が完了する前に追加のバッファリングが必要かどうかはわかりません。AVPlayerItemがソングの終わりまでバッファリングされたときを知る方法

現在の計画では、preferredForwardBufferDurationと組み合わせてアイテムをバッファリングする必要があるかどうかを確認しますが、これが最善の方法ですか?例えば

- (void)observeValueForKeyPath:(NSString*)aKeyPath ofObject:(id)aObject change:(NSDictionary*)aChange context:(void*)aContext 
{ 
    if([aKeyPath isEqualToString:@"playbackBufferFull"]) 
    { 
     CMTime theBufferTime = CMTimeMakeWithSeconds(self.currentItem.preferredForwardBufferDuration, 1); 
     CMTime theEndBufferTime = CMTimeAdd(self.currentItem.currentTime, theBufferTime); 
     if(CMTimeCompare(theEndBufferTime, self.currentItem.duration) >= 0) 
     { 
      // Buffered to the end 
     } 
    } 
} 

答えて

0

私は以下見ることができます。この問題にかなり良い解決策を見つけました。 preferredForwardBufferDurationがデフォルトで0に設定されているため、問題に書かれている提案された解決策はうまく機能しませんでした。

次のコードはかなりうまく機能します。私はタイマーで毎秒呼びます。

auto theLoadedRanges = self.currentItem.loadedTimeRanges; 

CMTime theTotalBufferedDuration = kCMTimeZero; 
for(NSValue* theRangeValue in theLoadedRanges) 
{ 
    auto theRange = [theRangeValue CMTimeRangeValue]; 
    theTotalBufferedDuration = CMTimeAdd(theTotalBufferedDuration, theRange.duration); 
} 

auto theDuration = CMTimeGetSeconds(self.currentItem.duration); 
if(theDuration > 0) 
{ 
    float thePercent = CMTimeGetSeconds(theTotalBufferedDuration)/theDuration; 
    if(thePercent >= 0.99f) 
    { 
     // Fully buffered 
    } 
} 
関連する問題