How to get file size and current file size from NSURL for AVPlayer iOS4.0

*爱你&永不变心* 提交于 2019-12-18 07:12:43

问题


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

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!


回答1:


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

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
}



回答2:


I think you do not want to know anything about file-sizes, but you're more interested in times.

Try self.player.currentItem.asset.duration for duration of currently playing item, self.player.currentTime for current time.




回答3:


@"loadedTimeRange" is a KVO value for the AVPlayerItem class. You can find its definition in the AVPlayerItem.h file in the

@interface AVPlayerItem (AVPlayerItemPlayability)

category definition.



来源:https://stackoverflow.com/questions/3999228/how-to-get-file-size-and-current-file-size-from-nsurl-for-avplayer-ios4-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!