7

I have an AVQueuePlayer (which obviously extends AVPlayer) that loads a playlist of streaming audio. Streaming is all working fine, but I'd like to have an activity indicator to show the user audio is loading. Trouble is, I can't seem to find any such Notification in AVQueuePlayer (or AVPlayer) that would indicate when the audio buffer has finished loading/is ready to play (nor does there appear to be a delegate method). Any thoughts?

Till
  • 27,559
  • 13
  • 88
  • 122
Keller
  • 17,051
  • 8
  • 55
  • 72

1 Answers1

26

You will have to use KVO to get this done.

For each item you are adding to the queue, you may setup observers like this:

item_ = [[AVPlayerItem playerItemWithURL:[NSURL URLWithString:@"http://somefunkyurl"]] retain];
[item_ addObserver:self forKeyPath:@"status" options:0 context:nil];
[item_ addObserver:self forKeyPath:@"playbackBufferEmpty" options:0 context:nil];

Now you can evaluate the status of that item within the observer method;

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([object isKindOfClass:[AVPlayerItem class]])
    {
        AVPlayerItem *item = (AVPlayerItem *)object;
        //playerItem status value changed?
        if ([keyPath isEqualToString:@"status"])
        {   //yes->check it...
            switch(item.status)
            {
                case AVPlayerItemStatusFailed:
                    NSLog(@"player item status failed");
                break;
                case AVPlayerItemStatusReadyToPlay:
                    NSLog(@"player item status is ready to play");
                break;
                case AVPlayerItemStatusUnknown:
                    NSLog(@"player item status is unknown");
                break;
            }
        }
        else if ([keyPath isEqualToString:@"playbackBufferEmpty"])
        {
            if (item.playbackBufferEmpty)
            {
                NSLog(@"player item playback buffer is empty");
            }
        }
    }
}
Till
  • 27,559
  • 13
  • 88
  • 122
  • 7
    You can also observe the playbackBufferEmpty property of the AVPlayerItem objects to detect if/when your buffer empties during playback so that you can show an activity indicator or 'buffering' warning. – reddersky Dec 06 '11 at 23:22
  • 2
    You could always up my comment if you liked it ;-) – reddersky Dec 07 '11 at 00:39
  • 2
    Minor remark: You should use the `context` to discern how to handle a KVO notification! It probably won't matter if you're subclassing `NSObject` but as soon as you're subclassing something a little more advanced than that, not forwarding `observeValueForKeyPath:...` can lead to the most peculiar side-effects. – danyowdee Dec 12 '11 at 09:57
  • @danyowdee you are so right - this really is just for the sake of the of this example. I shall never ever use the key path for checking if my observer is invoked for an event I actually intend to register. Using the context certainly is quicker and more reliable. – Till Dec 12 '11 at 10:18
  • Best way for remove this observer? – jose920405 Aug 18 '15 at 16:02
  • @Till , when should we stopAnimating our activity indicator? – Mohammad Zaid Pathan Oct 30 '15 at 06:30
  • 2
    @ZaidPathan you should start animating initially and whenever you got a `playbackBufferEmpty` and stop animating as soon as you get a `status` change. – Till Nov 01 '15 at 02:25
  • @reddersky / Till to detect when the buffer is empty you can also use AVPlayerItemPlaybackStalledNotification notification – Gabriel.Massana Jun 01 '16 at 13:14