13
我使用AVPlayer從URLAVPlayer狀態始終AVPlayerStatusReadyToPlay
在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的錯誤,它也必須進入這些內部。請幫忙!!
超級感謝:)你是救星:) – Srikanth 2015-11-05 04:49:45
謝謝!這段代碼幫助了我! – 2016-03-21 19:07:43
**準確**我在找什麼,謝謝! – zpasternack 2016-05-05 05:53:16