7

I'm using AVPlayer to play audio from remote server.

I want to display a progress bar showing the buffering progress and the "time played" progress, like the MPMoviePlayerController does when you play a movie.

Does the AVPlayer has any UI component that display this info? If not, how can I get this info (buffering state)?

Thanks

Pradeep Singh
  • 752
  • 1
  • 10
  • 23
Eyal
  • 10,777
  • 18
  • 78
  • 130

1 Answers1

20

Does the AVPlayer has any UI component that display this info?

No, there's no UI component for AVPlayer.

If not, how can I get this info (buffering state)?

You should observer AVPlayerItem.loadedTimeRanges

[yourPlayerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];

then using KVO to watch

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                    change:(NSDictionary *)change context:(void *)context {
    if(object == player.currentItem && [keyPath isEqualToString:@"loadedTimeRanges"]){
        NSArray *timeRanges = (NSArray*)[change objectForKey:NSKeyValueChangeNewKey];
        if (timeRanges && [timeRanges count]) {
            CMTimeRange timerange=[[timeRanges objectAtIndex:0]CMTimeRangeValue];
        }
    }
 }

timerange.duration is what you expect for.

And you have to draw buffer progress manually.

Refs here.

Community
  • 1
  • 1
saiday
  • 1,290
  • 12
  • 25
  • thanks for your reply, just to be sure, the 'timerange.duration' is the duration already loaded? or already played? And what if I pause the audio? will the buffering continue and the 'timerange.duration' will progress? – Eyal Dec 11 '12 at 10:57
  • Yes, `timerange.duration` shows you that AVPlayerItem loaded endpoint, otherwise `timerange.start` shows you startpoint. So, It's shows you loaded time not played time in CMTime type. – saiday Dec 13 '12 at 07:15
  • It works even your AVPlayerItem been paused, remote buffering and KVO will still work. – saiday Dec 13 '12 at 07:26