使用AVAudioPlayer框架完成播放后,如何再次播放相同的声音?

时间:2012-08-09 19:26:53

标签: objective-c ios avaudioplayer

我希望我的声音文件随后播放三次。但是,它只播放一次。 我想当第二次调用[crashSound play]时,声音还没有完成播放,因此任何后续调用都会丢失。我该如何解决这个问题。即,如何在完成当前播放后再次播放相同的文件?

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

    AVAudioPlayer *crashSound;

    // code...
}
@end

@implementation Game

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

    NSBundle *bundle = [NSBundle mainBundle];
    NSURL *crashSoundFile = [bundle URLForResource: @"crashSound" withExtension: @"wav"];
    crashSound = [[AVAudioPlayer alloc] initWithContentsOfURL:crashSoundFile error:NULL];

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

}
@end

1 个答案:

答案 0 :(得分:2)

我找到了一个解决方案,所以在这里我独自回答我自己的问题,以防任何人遇到同样的问题。 :)

通过在crashSound初始化下添加crashSound.numberOfLoops = 3;,crashSound将播放3次。

通过在crashSound初始化下添加crashSound.numberOfLoops = -1;,crashSound将无限期播放,直到调用[crashSound stop]方法。

https://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html