Last active
May 20, 2018 05:15
-
-
Save WLun001/d0206624e0f9863efcce9493111c9569 to your computer and use it in GitHub Desktop.
iOS 11 Swift 4 Play sound
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! | |
@IBAction func notePressed(_ sender: UIButton) { | |
playSound("note\(sender.tag)") | |
} | |
func playSound(_ soundFile: String) { | |
let soundURL = Bundle.main.url(forResource: soundFile, withExtension: "wav") | |
do{ | |
audioPlayer = try AVAudioPlayer(contentsOf: soundURL!) | |
} | |
catch { | |
print(error.localizedDescription) | |
} | |
audioPlayer.play() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment