Skip to content

Instantly share code, notes, and snippets.

@iannase
Created September 30, 2017 15:50
Show Gist options
  • Save iannase/546e31aa45862cfdc502ee9eb531bb30 to your computer and use it in GitHub Desktop.
Save iannase/546e31aa45862cfdc502ee9eb531bb30 to your computer and use it in GitHub Desktop.
import UIKit
import AVFoundation
class ViewController: UIViewController, AVAudioPlayerDelegate {
var audioPlayer: AVAudioPlayer!
let soundArray = ["note1.wav", "note2.wav", "note3.wav", "note4.wav", "note5.wav", "note6.wav", "note7.wav"]
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func notePressed(_ sender: UIButton) {
playSound(soundFileName: soundArray[sender.tag - 1])
}
func playSound(soundFileName : String) {
let soundUrl = Bundle.main.url(forAuxiliaryExecutable: soundFileName)
do {
audioPlayer = try AVAudioPlayer(contentsOf: soundUrl!)
} catch {
print(error)
}
audioPlayer.play()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment