1
votes

I am implementing AVPlayer (not AVAudioPlayer) and trying to play audio from URL. It is working fine. But as we know that sometimes we can have invalid URL (URL that does not have Audio byte). When I play audio from any URL AVPlayer status shows always AVPlayerStatusReadyToPlay which is wrong. For example : In URL part if give invalid URL like : http://soundx.mp3slash.net/indian/jhankar_beats/1%28mp3pk.com%29.mp3 (it does not works even in your browser) and even if you give https://stackguides.com/questions/ask or any site URL (which does not have audio bytes), AVPlayer shows always AVPlayerStatusReadyToPlay. I think in this case its status must AVPlayerItemStatusUnknown or AVPlayerStatusFailed Please help, how to know URL is invalid (AVPlayerItemStatusUnknown or AVPlayerStatusFailed) show that I can show message to user. Thanks in advance

-(void)playButtonClicked

{

        NSURL *url = [NSURL URLWithString:AUDIO_URL];
        [self.audioSliderBar setMinimumValue:AUDIO_SLIDER_BAR_0];
        playerItem = [AVPlayerItem playerItemWithURL:url];
        player = [AVPlayer playerWithPlayerItem:playerItem];
        player.volume=5.0;


        [player addObserver:self forKeyPath:STATUS options:0 context:nil];

        [playerItem addObserver:self forKeyPath:PLAY_BACK_BUFFER_EMPTY options:NSKeyValueObservingOptionNew context:nil];
        [playerItem addObserver:self forKeyPath:PLAY_BACK_LIKELY_TO_KEEP_UP options:NSKeyValueObservingOptionNew context:nil];

        addObserverFlag=YES;

}

    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:   (NSDictionary *)change context:(void *)context
{
    if (object == player && [keyPath isEqualToString:STATUS])
    {
        if (player.status == AVPlayerStatusFailed)
        {

          [ViewUtilities showAlert:AUDIO_PLAYER :AUDIO_PLAYER_FAILED_MESSAGE];

        }

        else if (player.status == AVPlayerStatusReadyToPlay)
        {

            else if (player.status == AVPlayerItemStatusUnknown)
        {
            [ViewUtilities showAlert:AUDIO_PLAYER :AUDIO_PLAYER_UNKNOWN];

        }

        if (!player)
        {
            return;
        }
    }
}`
1
You are not showing what you are doing with the AVPlayer. - gnasher729
The AVPlayer isn't going to check anything until you start playing, or for example use preroll. - gnasher729
@ gnasher729 , I have updated my code, now Have a look please. - Ravi

1 Answers

3
votes

AVPlayer Doc states

Possible values of the status property, to indicate whether it can successfully play items.

AVPlayerStatusReadyToPlay Indicates that the player is ready to play AVPlayerItem instances.

So I think that you need to check status of particular item via status property of type AVPlayerItemStatus