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

标签 objective-c ios avaudioplayer

我希望我的声音文件随后播放 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] 方法。

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

关于objective-c - 使用 AVAudioPlayer 框架完成播放后,如何再次播放相同的声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11890576/

相关文章:

ios - 在malloc_error_break中设置一个断点进行调试

ios - 如何获取 Facebook 页面的页面 ID

ios - UITableView 在 'Attempt to create two animations for cell' 中的同一批更新结果中重新加载和移动行

SwiftUI上出现AVAudioPlayer播放多次

ios - iOS 5和iOS 6中UITabbar的区别

iphone - 在 Xcode 中加速 NSTimer

ios - "[CALayer renderInContext]"在 iPhone X 上崩溃

ios - 带套接字的应用程序,显示警报返回到根 Controller

macos - 从哪里开始编写一个应用程序来修改 OS X 上所有声音的输出?

objective-c - AVAudioPlayer 仪表值仍然 - 160