audioPlayerDidFinishPlaying如何知道哪个文件完成了? [英] How does audioPlayerDidFinishPlaying know which file is finished?

查看:68
本文介绍了audioPlayerDidFinishPlaying如何知道哪个文件完成了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AVAudioPlayer播放15个音频文件.音频文件需要以5组为一组播放,每组有3个文件.一组中的三个文件不变.演出的顺序可能会改变.

I am using AVAudioPlayer to play a 15 audio files. The audio files need to be played in sets of 5 with each set having three files. The three files within a set do not change. The order that the sets are played may change.

我尝试使用isPlaying的while循环,如下所示:

I tried using a while loop with isPlaying like this:

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");
} 

我现在意识到这是错误的,其副作用是在播放文件的整个过程中整个用户界面都被锁定了.

I now realize that this is wrong with the side effect being that the entire UI is locked up for the duration of the file being played.

我知道我应该使用:

  • (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)播放器成功:(BOOL)标志

但是我不知道audioPlayerDidFinishPlaying应该如何确切地知道哪个文件已经完成播放.

But I do not know how audioPlayerDidFinishPlaying is supposed to know exactly which file has finished playing.

我认为可能是-(void)animationDidStop,您可以在其中使用valueForKeyPath来确定哪个文件已完成播放.

I thought it might be like - (void)animationDidStop where you can use a valueForKeyPath to figure out which file was finished playing.

是否必须手动跟踪?

任何将我指向正确方向的建议将不胜感激.

Any suggestions to point me in the right direction would be appreciated.

谢谢.

推荐答案

当AVAudioPlayer调用audioPlayerDidFinishPlaying方法时,它将自身作为参数提供.因此,您应该做的是检查URL,以找出刚刚播放完的文件.

When an AVAudioPlayer calls the audioPlayerDidFinishPlaying method, it supplies itself as an argument. So what you should do is check the URL to figure out which file just finished playing.

-(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
     }
}

这篇关于audioPlayerDidFinishPlaying如何知道哪个文件完成了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆