AVPlayer 状态 AVPlayerStatusFailed 无法正常工作

AVPlayer status AVPlayerStatusFailed not working Properly

我正在实现 AVPlayer(不是 AVAudioPlayer)并尝试播放来自 URL 的音频。它工作正常。但正如我们所知,有时我们可能会有无效的 URL(URL 没有音频字节)。 当我从任何 URL 播放音频时,AVPlayer 状态始终显示 AVPlayerStatusReadyToPlay,这是错误的。例如:在 URL 部分,如果给出无效的 URL,例如: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://whosebug.com/questions/ask 或任何站点 URL(没有音频字节), AVPlayer 始终显示 AVPlayerStatusReadyToPlay。 我认为在这种情况下它的状态必须是 AVPlayerItemStatusUnknown 或 AVPlayerStatusFailed 请帮忙,如何知道 URL 无效(AVPlayerItemStatusUnknown 或 AVPlayerStatusFailed)表明我可以向用户显示消息。 提前致谢

-(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;
        }
    }
}`

AVPlayer Doc 状态

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.

所以我认为您需要通过 AVPlayerItemStatus

类型的状态 属性 检查特定项目的状态