私は3つのボタンがあり、それぞれが異なるサウンドを再生するはずです。私の問題は、一度クリックした2番目のボタンも最初の音を鳴らすということです。 これを修正するために何かできることはありますか?2つの音が1つのボタンで瞬時に再生されます
import UIKit
import AVFoundation
class ViewController: UIViewController {
var audioPlayer = AVAudioPlayer()
var audioPlayer1 = AVAudioPlayer()
var audioPlayer2 = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
do {
audioPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "OhNo", ofType: "mp3")!))
audioPlayer.prepareToPlay()
}
catch{
print(error)
}
do {
audioPlayer1 = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "beep2", ofType: "mp3")!))
audioPlayer1.prepareToPlay()
}
catch{
print(error)
}
do {
audioPlayer2 = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "beep10", ofType: "mp3")!))
audioPlayer2.prepareToPlay()
}
catch{
print(error)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func catSound(_ sender: UIButton) {
audioPlayer.play()
}
@IBAction func beep(_ sender: UIButton) {
audioPlayer1.play()
}
@IBAction func beep10(_ sender: UIButton) {
audioPlayer2.play()
}
}