本文介绍了使用AVAudioPlayer框架完成播放后,如何再次播放相同的声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的声音文件随后播放三遍.但是,它只能播放一次.我猜想第二次调用[crashSound play]时,声音还没有结束播放,因此所有后续调用都丢失了.我该如何解决.即如何才能使同一文件在完成当前播放后再次播放?

I want my sound file to be played three times subsequently. However, it is only played once.I guess when the [crashSound play] is called for a second time the sound hasn't finished being played so any subsequent calls are lost. How can I fix that. i.e. How can I make it so that the same file is played again once it has finished its current 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

推荐答案

我找到了解决方案,所以在这里,我一个人回答我自己的问题,以防有人遇到相同的问题. :)

I found a solution so here I am all alone answering my own question in case anyone's having the same problem. :)

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

By adding crashSound.numberOfLoops = 3; underneath the crashSound initialization the crashSound will play 3 times.

,直到调用[crashSound stop]方法之前,crashSound将无限期播放.

by adding crashSound.numberOfLoops = -1; underneath the crashSound initialization the crashSound will play indefinitely until the [crashSound stop] method is called.

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

这篇关于使用AVAudioPlayer框架完成播放后,如何再次播放相同的声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 02:43