AVPlayer status AVPlayerStatusFailed not working Properly

痴心易碎 提交于 2021-01-28 17:35:25

问题


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://stackoverflow.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:


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



来源:https://stackoverflow.com/questions/32457562/avplayer-status-avplayerstatusfailed-not-working-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!