Skip to content

Instantly share code, notes, and snippets.

@konnnn
Last active March 6, 2020 20:33
Show Gist options
  • Save konnnn/1031a34cf2aefc01064e3e7c307de1b0 to your computer and use it in GitHub Desktop.
Save konnnn/1031a34cf2aefc01064e3e7c307de1b0 to your computer and use it in GitHub Desktop.
Play Sound Function
// MARK: Play sound function
func playSound(fileName: String) {
guard let url = Bundle.main.url(forResource: fileName, withExtension: "mp3") else {
print("the sound file isn't founded")
return
}
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
try AVAudioSession.sharedInstance().setActive(true)
// The following line is required for the player to work on iOS 11
player = try AVAudioPlayer(contentsOf: url)
guard let player = player else { return }
player.play()
} catch let error {
print(error.localizedDescription)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment