Aar*_*onG 1 iphone objective-c avaudioplayer ios
我正在使用AVAudioPlayer播放15个音频文件.音频文件需要以5个为一组播放,每组有3个文件.集合中的三个文件不会更改.播放集的顺序可能会发生变化.
我尝试使用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");
}
Run Code Online (Sandbox Code Playgroud)
我现在意识到副作用是错误的,因为整个UI在播放文件的持续时间内被锁定.
我知道我应该使用:
但我不知道audioPlayerDidFinishPlaying应该如何准确知道哪个文件已经播放完毕.
我认为它可能像 - (void)animationDidStop,你可以使用valueForKeyPath来确定哪个文件已经完成播放.
是否必须手动跟踪?
任何建议指出我正确的方向将不胜感激.
谢谢.
当AVAudioPlayer调用audioPlayerDidFinishPlaying方法时,它将自身作为参数提供.所以你应该做的是检查URL以确定刚刚播放完的文件.
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
if ([player.url isEqual:url1])
{
// file which is in url1 stopped playing
}
else if ([player.url isEqual:url2])
{
// file which is in url2 stopped playing
}
}
Run Code Online (Sandbox Code Playgroud)