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 のエラーを処理したいので、これらの中にも入る必要があります。助けてください!!