Objective c - Display buffering progress when playing audio from remote server

avaudioplayer, avplayer, ios, iphone, objective-c

Solution

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.

Problem

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

Original source

Related problems