xcode AVPlayer 状态总是 AVPlayerStatusReadyToPlay

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14933592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:47:50  来源:igfitidea点击:

AVPlayer status always AVPlayerStatusReadyToPlay

iosobjective-cxcodeavplayer

提问by user2082760

I am using AVPlayer to play audio from a URL

我正在使用 AVPlayer 从 URL 播放音频

In ViewDidLoad:

在 ViewDidLoad 中:

self.playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:imageText]];

self.player = [AVPlayer playerWithPlayerItem:playerItem];

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

[player play];

Observer

观察员

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                        change:(NSDictionary *)change context:(void *)context {
    if (object == player && [keyPath isEqualToString:@"status"]) {
        if (player.status == AVPlayerStatusReadyToPlay) {
            //[playingLbl setText:@"Playing Audio"];
            NSLog(@"fineee");
            [playBtn setEnabled:YES];
        } else if (player.status == AVPlayerStatusFailed) {
            // something went wrong. player.error should contain some information
            NSLog(@"not fineee");
            NSLog(@"%@",player.error);

        }
        else if (player.status == AVPlayerItemStatusUnknown) {
            NSLog(@"AVPlayer Unknown");


        }
    }
}

but the player sometimes is stuck and does not play the audio but then also status is AVPlayerStatusReadyToPlay. It never goes inside AVPlayerStatusFailed or AVPlayerItemStatusUnknown. As i want to handle AVPlayer's error, it must go inside these as well. Please help!!

但是播放器有时会卡住并且不播放音频,但状态也是 AVPlayerStatusReadyToPlay。它永远不会进入 AVPlayerStatusFailed 或 AVPlayerItemStatusUnknown。因为我想处理 AVPlayer 的错误,所以它也必须在这些里面。请帮忙!!

回答by saiday

You should observe CurrentItem's status. AVPlayer failed because of AVPlayerItem failed, if anythings went wrong, it begins from AVPlayerItem then AVPlayer.

您应该观察 CurrentItem 的状态。AVPlayer 失败,因为 AVPlayerItem 失败,如果出现任何问题,它从 AVPlayerItem 开始,然后是 AVPlayer。

try:

尝试:

[item addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];

in your observeValueForKeyPath:

在您的observeValueForKeyPath 中:

if (object == audioPlayer.currentItem && [keyPath isEqualToString:@"status"]) {
    if (audioPlayer.currentItem.status == AVPlayerItemStatusFailed) {
        NSLog(@"------player item failed:%@",audioPlayer.currentItem.error);
    }
}

You can take a look of AVPlayer's handling or use it directly from HysteriaPlayer, my open source project.

您可以查看 AVPlayer 的处理方式或直接从我的开源项目HysteriaPlayer 中使用它。