1
self.player = [[AVPlayer playerWithURL:[NSURL URLWithString:@"http://myurl.com/track.mp3"]] retain];

上記のトラックの UIProgressView を作成しようとしています。その URL からファイル サイズと現在のファイル サイズを取得するにはどうすればよいですか? 助けてください、ありがとう!

4

3 に答える 3

6

次のように、現在のアイテムの loadedTimeRanges プロパティの監視を開始する必要があります。

AVPlayerItem* playerItem = self.player.currentItem;
[playerItem addObserver:self forKeyPath:kLoadedTimeRanges options:NSKeyValueObservingOptionNew context:playerItemTimeRangesObservationContext];

次に、観測コールバックで、渡されたデータを次のように解釈します。

-(void)observeValueForKeyPath:(NSString*)aPath ofObject:(id)anObject change:(NSDictionary*)aChange context:(void*)aContext {

if (aContext == playerItemTimeRangesObservationContext) {

    AVPlayerItem* playerItem = (AVPlayerItem*)anObject;
    NSArray* times = playerItem.loadedTimeRanges;

    // there is only ever one NSValue in the array
    NSValue* value = [times objectAtIndex:0];

    CMTimeRange range;
    [value getValue:&range];
    float start = CMTimeGetSeconds(range.start);
    float duration = CMTimeGetSeconds(range.duration);

    _videoAvailable = start + duration; // this is a float property of my VC
    [self performSelectorOnMainThread:@selector(updateVideoAvailable) withObject:nil waitUntilDone:NO];
}

次に、メイン スレッドのセレクターが進行状況バーを次のように更新します。

-(void)updateVideoAvailable {

    CMTime playerDuration = [self playerItemDuration];
double duration = CMTimeGetSeconds(playerDuration);
    _videoAvailableBar.progress = _videoAvailable/duration;// this is a UIProgressView
}
于 2011-08-11T15:39:11.193 に答える
0

@"loadedTimeRange" は、AVPlayerItem クラスの KVO 値です。その定義は、次の AVPlayerItem.h ファイルにあります。

@interface AVPlayerItem (AVPlayerItemPlayability)

カテゴリー定義。

于 2013-07-23T17:44:59.427 に答える
0

ファイルサイズについては何も知りたくないと思いますが、時間の方が興味があります。

self.player.currentItem.asset.duration現在再生中のアイテムの継続時間、現在の時刻を試しself.player.currentTimeます。

于 2010-10-22T20:27:41.453 に答える