我希望我的声音文件随后播放 3 次。但是,它只播放一次。
我想当 [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
最佳答案
我找到了解决方案,所以在这里我独自一人回答我自己的问题,以防有人遇到同样的问题。 :)
通过在 crashSound 初始化下面添加 crashSound.numberOfLoops = 3;
,crashSound 将播放 3 次。
通过在 crashSound 初始化下面添加 crashSound.numberOfLoops = -1;
,crashSound 将无限期播放,直到调用 [crashSound stop]
方法。
关于objective-c - 使用 AVAudioPlayer 框架完成播放后,如何再次播放相同的声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11890576/