我有一个使用AVPlayer
从服务器传输音频的音乐播放器,它播放音频效果很好,但在完成音频后,当我点击新音频的播放按钮时,几乎需要时间来启动新音频35-40秒任何人都可以帮助我吗?
答案 0 :(得分:1)
使用观察者了解玩家状态。 使用KVO,可以通知玩家状态的变化:
playButton.enabled = NO;
player = [AVPlayer playerWithURL:fileURL];
[player addObserver:self forKeyPath:@"status" options:0 context:nil];
状态更改时将调用此方法:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context {
if (object == player && [keyPath isEqualToString:@"status"]) {
if (player.status == AVPlayerStatusReadyToPlay) {
playButton.enabled = YES;
} else if (player.status == AVPlayerStatusFailed) {
// something went wrong. player.error should contain some information
}
}
}