Hey! So I'm new to coding, and right now I'm trying to make a simple soundboard app on Xcode. I have 5 different sounds and 5 different buttons. I want each button to play one of those sounds. The problem is: All my buttons play the first sound. I can't find the problem. Please, help.
import UIKit
import AVFoundation
class ViewController: UIViewController {
let soundFilenames = ["sound1","sound2","sound3","sound4","sound5"]
var audioPlayers = [AVAudioPlayer]()
override func viewDidLoad() {
super.viewDidLoad()
/
/
for sound in soundFilenames {
do {
let audioPath = Bundle.main.path(forResource: sound, ofType: "wav")
let audioPlayer = try AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL)
audioPlayers.append(audioPlayer)
}
catch
{
audioPlayers.append(AVAudioPlayer())
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
/
}
@IBAction func buttonTapped(_ sender: UIButton) {
/
let audioPlayer = audioPlayers[sender.tag]
audioPlayer.play()
}
}