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 回答