iphone avaudiofoundation循环中的声音完成
问题描述:
在继续执行代码之前是否可以强制音频文件完成播放?在下面的例子中,我想要一个音频文件播放多次,因为我有数组中的项目。目前它完成循环只播放一次。我在AVFoundation框架内使用AVAudioPlayer。iphone avaudiofoundation循环中的声音完成
for (int i = 0; i<[Array count]; i++) {
if ([Array objectAtIndex:i] == @"Red") {
NSLog(@"red");
[self.player play];
}
if ([Array objectAtIndex:i] == @"Blue") {
NSLog(@"blue");
[self.player play];
}
if ([Array objectAtIndex:i] == @"Green") {
NSLog(@"green");
[self.player play];
}
if ([Array objectAtIndex:i] == @"Yellow") {
NSLog(@"yellow");
[self.player play];
}
}
我还使用方法audioPlayerDidFinishPlaying来测试它是否完成五次,但只有一次达到此方法。
-(void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL) completed{
if ((completed) == YES){
NSLog(@"audio finish");
return;
}
}
是否有可能在音频播放时保持for循环?
理论上控制台应改为:color->音频finish-> color->音频finish-> REPE
答
也许你能告诉AudioPlayer当您收到finishedPlaying通知喜欢玩:
int arrayIndex = 0
NSArray* array; //initialized somewhere..
-(void)myFunction
{
if(arrayIndex < [array length])
{
NSArray* colors = [NSArray arrayWithObjects:@"Red",@"Blue",@"Green",@"Yellow",nil];
if([colors indexOf:[array objectAtIndex:arrayIndex]] != NSNotFound)
{
NSLog([array objectAtIndex:arrayIndex]);
[self.player play];
}
arrayIndex++;
}else{
NSLog(@"audio finish");
}
}
-(void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL) completed{
if ((completed) == YES){
[self myFunction];
}
}
-(void)start_it_up
{
arrayIndex = 0;
[self myFunction];
}
我喜欢这个想法。我会试一试。 – 2011-06-17 13:06:37
这让我走上正轨!谢谢你的帮助。 – 2011-06-20 19:27:29