Skip to content

Instantly share code, notes, and snippets.

@Whatapalaver
Last active December 24, 2018 16:11
Show Gist options
  • Save Whatapalaver/6dddffaa5089d3a24817fc7bfdb39f73 to your computer and use it in GitHub Desktop.
Save Whatapalaver/6dddffaa5089d3a24817fc7bfdb39f73 to your computer and use it in GitHub Desktop.
Swift - Recipes
import UIKit
import AVFoundation
class ViewController: UIViewController, AVAudioPlayerDelegate {
var audioPlayer : AVAudioPlayer!
let soundArray = ["note1", "note2", "note3", "note4", "note5", "note6", "note7"]
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(forResource: soundFileName, withExtension: "wav")
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