是否可以在继续编写代码之前强制播放完音频文件?在下面的例子中,我希望一个音频文件可以播放数组中的所有项。当前它只完成循环播放一次。我在AVFoundation框架中使用AVAudioPlayer。
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循环保持在适当位置?
理论上,控制台应该是:颜色->音频完成->颜色->音频完成->repe
发布于 2011-06-17 04:38:46
也许你可以在收到finishedPlaying通知时告诉AudioPlayer播放,如下所示:
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];
} https://stackoverflow.com/questions/6377261
复制相似问题