【发布时间】:2011-03-17 17:49:28
【问题描述】:
我正在使用 AVAudioPlayer 播放 15 个音频文件。音频文件需要以 5 个为一组播放,每组包含三个文件。一组中的三个文件不会更改。套牌的播放顺序可能会改变。
我尝试使用带有 isPlaying 的 while 循环,如下所示:
while ([backgroundSound isPlaying]) {
NSLog(@"First while loop");
}
backgroundSoundPath = [[NSBundle mainBundle] pathForResource:@"Set0File1" ofType:@"mp3"]; // *Music filename*
backgroundSound =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:backgroundSoundPath] error:nil];
[backgroundSound prepareToPlay];
[backgroundSound play];
while ([backgroundSound isPlaying]) {
NSLog(@"Second while loop");
}
backgroundSoundPath = [[NSBundle mainBundle] pathForResource:@"Set0File2" ofType:@"mp3"]; // *Music filename*
backgroundSound =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:backgroundSoundPath] error:nil];
[backgroundSound prepareToPlay];
[backgroundSound play];
while ([backgroundSound isPlaying]) {
NSLog(@"Third while loop");
}
我现在意识到这是错误的副作用,即整个 UI 在文件播放期间被锁定。
我知道我应该使用:
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)播放成功:(BOOL)flag
但我不知道 audioPlayerDidFinishPlaying 应该如何确切知道哪个文件已完成播放。
我认为它可能类似于 - (void)animationDidStop,您可以在其中使用 valueForKeyPath 来确定播放完毕的文件。
是否必须手动跟踪?
任何能指引我正确方向的建议将不胜感激。
谢谢。
【问题讨论】:
标签: iphone objective-c ios avaudioplayer