Reputation: 11
I have been trying to program a soundboard app, which of course has multiple sounds in it, so far I have two buttons and two sounds, but each button plays the same sound. I am a noob programmer, so please explain to me in detail what I need to do. I apologize for my formatting, as this is my first question being asked one stack overflow, everything below is code. I currently have 2 buttons on my storyboard. Help would be appreciated. Below is my ViewController.swift
import UIKit
import AVFoundation
class ViewController: UIViewController {
var sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("button", ofType: "wav")!)
var sound2 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sweg", ofType: "wav")!)
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
audioPlayer = AVAudioPlayer(contentsOfURL: sound, error: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func soundbutton(sender: UIButton) {
audioPlayer.play()
}
@IBAction func sound2(sender: UIButton) {
audioPlayer.play()
}
}
Upvotes: 1
Views: 1031
Reputation: 791
You should use a single IBAction for those buttons ex
@IBAction func soundButton (sender: UIButton){
switch sender.currentTitle! {
case "sound1"://here you put the title the first button has
audioPlayer = AVAudioPlayer(contentsOfURL: sound, error: nil)
audioPlayer.play()
case "sound2"://here you put the title the second button has,here sound2 was an example,because IDK your buttons titles
audioPlayer = AVAudioPlayer(contentsOfURL: sound2, error: nil)
audioPlayer.play()
default : break
}
}
currentTitle is the title of the button,so if the first button title is 😆,you replace sound1 with 😆.
No joke emoji work :)):D
hope that this helps
Edit
The wrong thing that you were doing is that you set in the viewDidload audioPlayer
to sound, and thats why you had just one sound
You can remove that line from viewDidLoad
Edit: inside the class This is what you need inside the ViewController,nothing more,nothing less(),make sure you connect the buttons to the IBAction and disconnect from other methods
import UIKit
import AVFoundation
class ViewController: UIViewController {
var sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("button", ofType: "wav")!)
var sound2 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sweg", ofType: "wav")!)
var audioPlayer = AVAudioPlayer()
@IBAction func soundButton (sender: UIButton){
switch sender.currentTitle! {
case "sound1"://here you put the title the first button has
audioPlayer = AVAudioPlayer(contentsOfURL: sound, error: nil)
audioPlayer.play()
case "sound2"://here you put the title the second button has,here sound2 was an example,because IDK your buttons titles
audioPlayer = AVAudioPlayer(contentsOfURL: sound2, error: nil)
audioPlayer.play()
default : break
}
}
}
the most simple implementation
import UIKit
import AVFoundation
class ViewController: UIViewController {
var audioPlayer = AVAudioPlayer()
@IBAction func soundButton (sender: UIButton){
if let title = sender.currentTitle{
switch title {
case "sound1"://here you put the title the first button has
audioPlayer = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("button", ofType: "wav")!), error: nil)
audioPlayer.play()
case "sound2"://here you put the title the second button has,here sound2 was an example,because IDK your buttons titles
audioPlayer = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sweg", ofType: "wav")!), error: nil)
audioPlayer.play()
default : break
}
}
}
}
Now the only error you will have is SIGABART because that buttons have some outlets and actions
Upvotes: 1
Reputation: 2411
import UIKit
import AVFoundation
class ViewController: UIViewController {
var sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("button", ofType: "wav")!)
var sound2 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sweg", ofType: "wav")!)
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func soundbutton(sender: UIButton) {
audioPlayer = AVAudioPlayer(contentsOfURL: sound, error: nil)
audioPlayer.play()
}
@IBAction func sound2(sender: UIButton) {
audioPlayer = AVAudioPlayer(contentsOfURL: sound2, error: nil)
audioPlayer.play()
}
}
Upvotes: 0
Reputation: 12667
you need to initialise another AVAudioPlayer with another sound, and play it on second button tap.
Upvotes: 0