如何在使用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]
方法。
- 重绘视图closures屏幕 – 包含select器视图和工具栏
- 在Google地图中点击标记时显示信息窗口在实现mapView时执行iOS sdk:didTapMarker:实现了delegate方法
- audio文件不能在iPhone 5中播放
- 在locking屏幕上设置和访问iOS查找栏
- AVAudioPlayer – 你必须创build一个属性才能工作? (Xcode中)
- 应用程序:didReceiveRemoteNotification:fetchCompletionHandler:调用时不在UIApplicateStateBackground中
- 如何在iOS上使用AVAudioPlayer播放指定持续时间的声音?
- iPhone SDK:从一个button播放一个WAV
- Swift 3 iOS – 与AVAudioPlayer同步暂停
- 如何阻止AVAudioPlayer在数据到达之前重复执行?
- AVAudioPlayer帮助:同时播放多个声音,一次停止所有声音,以及解决自动参考计数问题