1

我正在实现 AVPlayer(不是 AVAudioPlayer)并尝试从 URL 播放音频。它工作正常。但正如我们所知,有时我们可能会有无效的 URL(没有音频字节的 URL)。当我从任何 URL 播放音频时,AVPlayer 状态总是显示 AVPlayerStatusReadyToPlay这是错误的。例如:在 URL 部分,如果给出无效的 URL,例如:http ://soundx.mp3slash.net/indian/jhankar_beats/1%28mp3pk.com%29.mp3 (即使在您的浏览器中也不起作用) ,即使您给出https://stackoverflow.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;
        }
    }
}`
4

1 回答 1

3

AVPlayer 文档状态

status 属性的可能值,表示是否可以成功播放项目

AVPlayerStatusReadyToPlay 指示播放器已准备好播放AVPlayerItem 实例。

所以我认为您需要通过AVPlayerItemStatus类型的 status 属性检查特定项目的状态

于 2015-09-08T12:33:40.767 回答