This code has 4 buttons; 2 buttons play different sounds and 2 buttons stop the sound. Only one button can play its sound at a time. Once another button is pressed the sound from the button that was pressed is stopped for the new button to play its song. How make my code play 2 buttons at the same time? Code is below thanks?
mport UIKit
import AVFoundation
class ViewController: UIViewController {
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func hatingschool(sender: AnyObject) {
let alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("s", ofType: "mp3")!)
do {
// Preperation
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
} catch _ {
}
do {
try AVAudioSession.sharedInstance().setActive(true)
} catch _ {
}
// Play the sound
do {
audioPlayer = try AVAudioPlayer(contentsOfURL: alertSound)
audioPlayer.numberOfLoops = -1
} catch _{
}
audioPlayer.prepareToPlay()
audioPlayer.play()
}
@IBAction func stophatingschool(sender: AnyObject) {
audioPlayer.stop()
audioPlayer.currentTime = 0
}
@IBAction func drums(sender: AnyObject) {
let dd = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("dsd", ofType: "mp3")!)
do {
// Preperation
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
} catch _ {
}
do {
try AVAudioSession.sharedInstance().setActive(true)
} catch _ {
}
// Play the sound
do {
audioPlayer = try AVAudioPlayer(contentsOfURL: dd)
audioPlayer.numberOfLoops = -1
} catch _{
}
audioPlayer.prepareToPlay()
audioPlayer.play()
}
@IBAction func stopdrums(sender: AnyObject) {
audioPlayer.stop()
audioPlayer.currentTime = 0
}
}