我遇到了AVAudioPlayer类的奇怪问题,有时未调用audioPlayerDidFinishPlaying:successfully:
回调。开始播放的精简代码看起来像这样,
self.player = [[[AVAudioPlayer alloc] initWithContentsOfURL:localFileAudioURL error:&error] autorelease];
self.player.delegate = self;
[player prepareToPlay];
[NSTimer scheduledTimerWithTimeInterval:0.016f target:self selector:@selector(updatePlaybackProgress) userInfo:nil repeats:YES];
[player play];
有一个计时器,每16毫秒运行一次,以显示播放进度。每当音频播放结束并且调用
audioPlayerDidFinishPlaying:successfully:
回调时,计时器将失效。回调代码是,- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
[self.progressTimer invalidate];
self.progressTimer = nil;
}
但是,偶尔(一次播放20-30次)此回调不会被调用。我添加了断点并记录了调用,以验证未调用此回调方法。
要播放的numberOfLoops设置为
0
,因此不再重复。如果有帮助,这里还有一些其他信息。正在创建的重复计时器将使用当前播放进度更新UI。它的代码看起来像
- (void)updatePlaybackProgress {
SoundCell *soundCell = [self soundCellForCurrentlyPlayingSound];
NSTimeInterval progress = player.currentTime / player.duration;
if (progress >= 0) {
soundCell.progress = progress;
}
}
有时不会调用完成回调,因此计时器不会无效,并且每16毫秒调用一次。我在
player.currentTime
方法中记录了progress
和updatePlaybackProgress
的值,看来回放一遍又一遍。 currentTime
的值从0.0
到1.0
不断增加,然后又回到0.0
并重新开始。但是,音频只会在第一次播放,而不会按照currentTime
的值连续播放。对于持续时间为1秒到30秒以上的声音,此问题已经发生。
目前我还没有主意,Google和Apple开发者论坛没有任何关联。
最佳答案
响应声音播放完成
– audioPlayerDidFinishPlaying:成功:
响应音频解码错误
– audioPlayerDecodeErrorDidOccur:错误:
处理音频中断
– audioPlayerBeginInterruption:
– audioPlayerEndInterruption:
– audioPlayerEndInterruption:withFlags:
播放停止时,很可能会调用其中之一。您还可以注册NSNotifications进行音频播放,在实现方法中,您只需要将音频播放器与本地播放器进行比较,就可以知道您对此感兴趣。