我正在使用 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 的错误,它也必须进入这些内部。请帮忙!!