如何在同一时间停止播放2种不同的音频?当音乐播放时,另一个按钮同时按下播放音频。对不起我的英语不好。如何在同一时间停止播放2种不同的音频?
NoiseMaker
:
import AVFoundation
class NoiseMaker {
let audioFileNames = ["guitar", "applause", "monster", "bubbles"]
let players: [AVAudioPlayer?]
init() {
players = audioFileNames.map { filename in
if let url = NSBundle.mainBundle().URLForResource(filename, withExtension: "wav") {
return try? AVAudioPlayer(contentsOfURL: url)
} else {
return nil
}
}
}
func play(index: Int) {
if !players.isEmpty && index >= 0 && index < players.count {
players[index]?.play()
}
}
}
ViewController
:
import UIKit
class ViewController: UIViewController {
let noiseMaker = NoiseMaker()
@IBAction func playSound(sender: UIButton) {
noiseMaker.play(sender.tag)
}
}
不要发布链接到代码,使用代码标签,并把你的代码正确的在你的问题。 –