Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 414
Eyal Avatar asked Dec 10 '12 18:12

Eyal


1 Answers

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.

like image 66
saiday Avatar answered Sep 28 '22 07:09

saiday