Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVPlayer Observer never called for an invalid URL

Tags:

ios

avplayer

I'm trying to do a basic radio app and I got a list of URL: when I try to call an invalid URL (wrong path or a right path with no playable file) the Observer seems to be never called. Here is part of my code:

        urlStream = [NSURL URLWithString:mp3URL];  

        self.playerItem = [AVPlayerItem playerItemWithURL:urlStream];

        [playerItem addObserver:self forKeyPath:@"playbackBufferEmpty"   options:NSKeyValueObservingOptionNew context:nil];
        [playerItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];
        [appDelegate.player addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];

        [appDelegate.player replaceCurrentItemWithPlayerItem:playerItem]; //OK

        [appDelegate.player play];

and in the relative observer method I got:

    if (object == playerItem && [keyPath isEqualToString:@"playbackBufferEmpty"])
{
    if (playerItem.playbackBufferEmpty) 
    {
        NSLog(@"playbackBufferEmpty");
        [self alertBuffer];
    }

}

if (object == playerItem && [keyPath isEqualToString:@"playbackLikelyToKeepUp"])
{
    if (playerItem.playbackLikelyToKeepUp)
    {
        NSLog(@"playbackLikelyToKeepUp");
        [loadStation stopAnimating];

        if (playerItem.status == AVPlayerItemStatusReadyToPlay) {
            NSLog(@"AVPlayerItemStatusReadyToPlay");
        }
        else if (playerItem.status == AVPlayerStatusFailed) {
            NSLog(@"AVPlayerStatusFailed");
        }
        else if (playerItem.status == AVPlayerStatusUnknown) {
            NSLog(@"AVPlayerStatusUnknown");
        }
    }
}

else if (object == appDelegate.player && [keyPath isEqualToString:@"status"]) {
    if (appDelegate.player.status == AVPlayerStatusReadyToPlay) {
        NSLog(@"Player Status = Ready to Play");

    } 
    if (appDelegate.player.status == AVPlayerStatusUnknown) {
        NSLog(@"Player Status = Sometimes did wrong.");
        [self alertUnknown];
    }
    if (appDelegate.player.status == AVPlayerStatusFailed) {
        NSLog(@"Player Status = Status Failed.");
        [self alertStatusFailed];
    }

}

Whatever URL I call I only got the status ReadyToPlay: when I choose the invalid URL nothing happens. An example of URL not working is: http://audioplayer.wunderground.com:80/RHBrant/Cheyenne.mp3.m3u

Where am I wrong?

Thank you VERY much.

like image 951
Fabrizio Avatar asked Mar 20 '12 11:03

Fabrizio


1 Answers

Ok I found my mistake: I was adding observer to appDelegate.player to catch for bad URL when I all had to do was adding to playerItem:

        [playerItem addObserver:self forKeyPath:@"status" options:0 context:nil];

and in the relative method:

         if ([playerItem status] == AVPlayerStatusFailed) {
             NSLog(@"playerItem Status = Failed.");
             NSLog(@"Error = %@",error.description);
             return;
         }

Now it's ok.

Thanx

like image 87
Fabrizio Avatar answered Sep 23 '22 03:09

Fabrizio