Ich habe drei Tasten und jeder soll einen anderen Klang spielen, die sie tun. Mein Problem ist, dass der zweite Knopf einmal den ersten Ton spielt. Gibt es etwas, was ich tun könnte, um das zu beheben?zwei Sounds spielen auf einer Taste in swift
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()
}
}