首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iphone completion completion声音在循环中完成

iphone completion completion声音在循环中完成
EN

Stack Overflow用户
提问于 2011-06-17 03:12:03
回答 1查看 211关注 0票数 0

是否可以在继续编写代码之前强制播放完音频文件?在下面的例子中,我希望一个音频文件可以播放数组中的所有项。当前它只完成循环播放一次。我在AVFoundation框架中使用AVAudioPlayer。

代码语言:javascript
复制
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来测试它是否完成了五次,但它只到达该方法一次。

代码语言:javascript
复制
-(void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL) completed{
    if ((completed) == YES){
        NSLog(@"audio finish");
        return;
    }
}

是否可以在播放音频时将for循环保持在适当位置?

理论上,控制台应该是:颜色->音频完成->颜色->音频完成->repe

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-06-17 04:38:46

也许你可以在收到finishedPlaying通知时告诉AudioPlayer播放,如下所示:

代码语言:javascript
复制
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];
} 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6377261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档