playableDuration は、次の手順で大まかに実装できます。
- (NSTimeInterval) playableDuration
{
// use loadedTimeRanges to compute playableDuration.
AVPlayerItem * item = _moviePlayer.currentItem;
if (item.status == AVPlayerItemStatusReadyToPlay) {
NSArray * timeRangeArray = item.loadedTimeRanges;
CMTimeRange aTimeRange = [[timeRangeArray objectAtIndex:0] CMTimeRangeValue];
double startTime = CMTimeGetSeconds(aTimeRange.start);
double loadedDuration = CMTimeGetSeconds(aTimeRange.duration);
// FIXME: shoule we sum up all sections to have a total playable duration,
// or we just use first section as whole?
NSLog(@"get time range, its start is %f seconds, its duration is %f seconds.", startTime, loadedDuration);
return (NSTimeInterval)(startTime + loadedDuration);
}
else
{
return(CMTimeGetSeconds(kCMTimeInvalid));
}
}
_moviePlayer は AVPlayer インスタンスです。AVPlayerItem の loadedTimeRanges をチェックすることで、推定の playableDuration を計算できます。
セクションが 1 つしかないビデオの場合は、この手順を使用できます。ただし、複数セクションのビデオの場合は、loadedTimeRagnes の配列内のすべての時間範囲をチェックして、正しい答えを得ることができます。