如何在iOS应用程序中循环播放声音文件?

时间:2014-05-21 11:10:37

标签: ios audio mp3 avaudioplayer

iPhone的应用程序,我想要一致地重现多个音频文件(与显示图像并行)。我将名称放入音频文件和图像文件的数组中。但是当我在启动应用程序后尝试播放循环图像和声音时,所有声音都会同时播放。我试图实现单独的启动(使用dispatch_after),但是在循环中的12个步骤发生一些制动。 我认为有任何方法可以暂停。直到当前文件播放循环结束才进行下一步。但我不知道它是如何实施的。有人可以提供建议或示例代码如何解决? 附:我读到了有关audioPlayerDidFinishPlaying的内容,但无法弄清楚如何在实践中使用此方法。

for (i = 0; i <= 21; i++)
  {
   double delayInSeconds = i * 4.5;
   DZpopTimeStep *tempVar = [dataPopTime objectAtIndex: i];
   tempVar.popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
   dispatch_after(tempVar.popTime, dispatch_get_main_queue(), ^(void)
           {
               //.....image
               UIGraphicsBeginImageContext(self.imageFromFile.frame.size);
               [[UIImage imageNamed:tempVar.nameColorFile] drawInRect:self.view.bounds];
               UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
               UIGraphicsEndImageContext();
               UIColor *fonMain = [UIColor colorWithPatternImage:image];
               imageFromFile.backgroundColor = fonMain;

               // ... play of sound file
               [self playSoundFile:(NSString *)tempVar.nameSoundFile];
           });
   } 

1 个答案:

答案 0 :(得分:0)

查看AVQueuePlayer,它似乎是您正在寻找的,这里是它的链接: https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVQueuePlayer_Class/Reference/Reference.html