如何从NSURL获取AVPlayer iOS4.0的文件大小和当前文件大小 [英] How to get file size and current file size from NSURL for AVPlayer iOS4.0

查看:152
本文介绍了如何从NSURL获取AVPlayer iOS4.0的文件大小和当前文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在尝试为上述曲目制作UIProgressView。如何从该URL获取文件大小和当前文件大小?请帮助,谢谢!

I am trying make a UIProgressView for the above track. How do I obtain the file size and current file size from that URL? Please help, thanks!

推荐答案

您需要开始观察当前项目的loadedTimeRanges属性,如下所示:

You need to start observing the loadedTimeRanges property of the current item, like this:

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

然后,在观察回调中,您可以理解您传递的数据,如下所示: / p>

Then, in the observation callback, you make sense of the data you're passed like this:

-(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];
}

然后主线程上的选择器更新进度条,如下所示:

Then the selector on the main thread updates a progress bar, like so:

-(void)updateVideoAvailable {

    CMTime playerDuration = [self playerItemDuration];
double duration = CMTimeGetSeconds(playerDuration);
    _videoAvailableBar.progress = _videoAvailable/duration;// this is a UIProgressView
}

这篇关于如何从NSURL获取AVPlayer iOS4.0的文件大小和当前文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆