9

MPMoviePlayerController には、playableDuration というプロパティがあります。

playableDuration現在再生可能なコンテンツの量 (読み取り専用)。

@property (非アトミック、読み取り専用) NSTimeInterval playableDuration

プログレッシブ ダウンロードのネットワーク コンテンツの場合、このプロパティは現在再生できるコンテンツの量を反映します。

AVPlayer に似たようなものはありますか? Apple Docs または Google で何も見つかりません (ここ Stackoverflow.com でも)

前もって感謝します。

4

5 に答える 5

17

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 の配列内のすべての時間範囲をチェックして、正しい答えを得ることができます。

于 2012-09-04T09:37:26.840 に答える
2

AVPlayer がメディア ファイルを再生する準備が整ったことを検出する必要があります。これを行う方法がわからない場合は、お知らせください。

ただし、メディア ファイルが読み込まれると、次の方法を使用できます。

#import <AVFoundation/AVFoundation.h>

/**
 * Get the duration for the currently set AVPlayer's item. 
 */
- (CMTime)playerItemDuration {
    AVPlayerItem *playerItem = [mPlayer currentItem];
    if (playerItem.status == AVPlayerItemStatusReadyToPlay) {
        return [[playerItem asset] duration];
    }
    return(kCMTimeInvalid);
}

このメソッドを使用する場合、(コンテンツをストリーミングしているため) 長さの値が無効か何かである可能性があることを理解することが重要です。したがって、処理に使用する前にこれを確認する必要があります。

CMTime playerDuration = [self playerItemDuration];
if (CMTIME_IS_INVALID(playerDuration)) {
    return;
}
double duration = CMTimeGetSeconds(playerDuration);
于 2011-08-05T15:57:00.583 に答える