Last active
December 24, 2018 16:11
-
-
Save Whatapalaver/6dddffaa5089d3a24817fc7bfdb39f73 to your computer and use it in GitHub Desktop.
Swift - Recipes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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