ios - 使用 AVAudioPlayer 相互播放声音

标签 ios audio avaudioplayer

好吧,抱歉,如果我看起来像个菜鸟,但我是 xcode 新手。我目前正在制作一个音板,并且我已经拥有所有可以工作和播放声音的按钮。

但是,如果正在播放一个声音,当我按下另一个按钮时,它不会停止原始声音,而是会播放它,所以我在徘徊是否有人知道如何解决这个问题?

这是我用于按钮的代码:

- (IBAction)playsound {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Gold" ofType:@"mp3"];
    AVAudioPlayer* myAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    myAudio.delegate = self;
    myAudio.volume = 2.0;
    myAudio.numberOfLoops = 0;
    [myAudio play];
}

如果有人能帮助我,我将非常感激。

谢谢。

最佳答案

如果其他声音仍在播放,您需要告诉它停止。您可能想研究在另一个 AVAudioPlayer 上调用 stop 方法。所以,就像你所拥有的那样:

[myAudio play];

...在此之前插入一个方法调用,以告诉其他声音停止。

[myOtherAudioThatsPlaying stop];
[myAudio play];

浏览 AVAudioPlayer documentation 可能也有意义。 .

关于ios - 使用 AVAudioPlayer 相互播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5107298/

相关文章:

ios - 应用程序中的自动布局 RTL 支持

ios - 在 viewWillAppear 中设置约束

ios - iPhone 和 iPad 上的边到边对象

java - 将 URL 与 AudioInputStream 结合使用

python - Try子句中的语音识别和Shutil问题

iphone - 使用 AVAudioPlayer 时删除状态栏中的播放图标

c# - MonoMac - AVAudioPlayer 崩溃

iphone - 根据 iPhone/xcode 上按下的按钮播放声音

c# - 单点触控 : trying to play audio from a file that is generated on the fly using AVAudioPlayer

ios - Swift:打印出 UILabel.text 时出现问题