ios – AVPlayer状态总是AVPlayerStatusReadyToPlay

ios – AVPlayer状态总是AVPlayerStatusReadyToPlay,第1张

概述我正在使用AVPlayer从URL播放音频 在ViewDidLoad中: self.playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:imageText]];self.player = [AVPlayer playerWithPlayerItem:playerItem];[player addObserver 我正在使用AVPlayer从URL播放音频

在VIEwDIDLoad中:

self.playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:imageText]];self.player = [AVPlayer playerWithPlayerItem:playerItem];[player addobserver:self forKeyPath:@"status" options:0 context:nil];[player play];

观察

- (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");        }    }}

但播放器有时被卡住,并且不播放音频,但是状态是AVPlayerStatusReadytoplay.它永远不会进入AVPlayerStatusFailed或AVPlayerItemStatusUnkNown.因为我想处理AVPlayer的错误,它也必须在这些内.请帮忙!!

解决方法 您应该遵守CurrentItem的状态. AVPlayer失败,因为AVPlayerItem失败,如果anythings出错,它从AVPlayerItem然后AVPlayer开始.

尝试:

[item addobserver:self forKeyPath:@"status" options:NSkeyvalueObservingOptionNew context:nil];

在你的observeValueForKeyPath中:

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

您可以看看AVPlayer的处理或直接从HysteriaPlayer开源项目使用它.

总结

以上是内存溢出为你收集整理的ios – AVPlayer状态总是AVPlayerStatusReadyToPlay全部内容,希望文章能够帮你解决ios – AVPlayer状态总是AVPlayerStatusReadyToPlay所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1111134.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存