iphone avaudiofoundation声音完成循环

时间:2011-06-16 19:12:03

标签: iphone ios4 audio avaudioplayer avfoundation

在继续使用代码之前是否可以强制播放音频文件?在下面的例子中,我想要播放音频文件的次数与数组中的项目一样多。当前它完成循环只播放一次。我在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循环保持在原位?

理论上控制台应该是:color-&gt; audio finish-&gt; color-&gt; audio finish-&gt; repe

1 个答案:

答案 0 :(得分:1)

也许你可以在收到完成的播放通知时告诉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];
}