Last active
March 6, 2020 20:33
-
-
Save konnnn/1031a34cf2aefc01064e3e7c307de1b0 to your computer and use it in GitHub Desktop.
Play Sound Function
This file contains 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
// 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