Skip to content

Instantly share code, notes, and snippets.

@ahbou
Created October 12, 2017 08:18
Show Gist options
  • Select an option

  • Save ahbou/064e4012397e9053e5ee3dc844ca2fcf to your computer and use it in GitHub Desktop.

Select an option

Save ahbou/064e4012397e9053e5ee3dc844ca2fcf to your computer and use it in GitHub Desktop.
Play .wav file in Swift
let ringtonePath = URL(fileURLWithPath: Bundle.main.path(forResource: "sound", ofType: "wav")!)
do {
let ringtonePlayer = try AVAudioPlayer(contentsOf: ringtonePath)
try AVAudioSession.sharedInstance().setActive(true)
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)
ringtonePlayer.volume = 1.0
ringtonePlayer.play()
} catch {
print("Failed to initialize audio player \(error.localizedDescription)")
}
@amizerov
Copy link
Copy Markdown

amizerov commented Sep 5, 2021

does't work for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment