在某些情况下,如何使用AVAudioPlayer框架在这里循环播放声音?

时间:2012-08-09 16:12:47

标签: objective-c ios core-audio

编辑:现在尝试实施Adam B的答案......

我有一个crashSound.wav文件,我把它放在我的xCode项目的Supporting Files文件夹中

我现在正试图让它在while循环中播放,但文档不是很清楚我究竟能做到这一点。我知道我必须在某个地方创建一个委托对象(我猜我的Game类)并获得有关stopButtonPressed是否为false以及文件是否已播放以便在stopButtonPressed条件为false时可以循环播放的通知。我知道我不应该通过调用[crashSound play]方法来做到这一点,但我不知道该怎么做..有什么帮助吗?

@interface Game()
{
    // code...

    AVAudioPlayer *crashSound;

    // code...
}
@end

@implementation Game

- (id) init 
{ 
    // code...

    NSURL *crashSoundFile = [[NSURL alloc] initWithString:@"crashSound" ];

    crashSound = [[AVAudioPlayer alloc] initWithContentsOfURL:crashSoundFile error:NULL];

    // code...
}
-(void) play // this is my "main" method that will be called once the playButton is pressed
{
   while(!self.stopButonPressed)
   {
      [crashSound play];
   }
}
@end

1 个答案:

答案 0 :(得分:0)

您构建代码的错误是因为大多数多媒体库的工作方式(例如,包括AVPlayer)。

您可以开始播放声音,而不是使用while循环,并在每次完成时检查您的条件是否为真。您可能需要根据计时器或声音完成时的回调触发“下一个”事物,而不是循环。

有关如何设置AVPlayer和通知功能playerItemDidReachEnd:的示例,请参阅此问题和答案:Looping a video with AVFoundation AVPlayer?