如何在使用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 

我find了一个解决scheme,所以在这里,我独自回答自己的问题,以防万一有人遇到同样的问题。 🙂

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

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

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