Skip to content

Instantly share code, notes, and snippets.

@carlynorama
Created September 3, 2020 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlynorama/d737e9cd8a2d4faeaf71b96aca56f79d to your computer and use it in GitHub Desktop.
Save carlynorama/d737e9cd8a2d4faeaf71b96aca56f79d to your computer and use it in GitHub Desktop.
Playing sounds
import AVFoundation
// ---------- Custom Sounds ----------
//https://www.hackingwithswift.com/example-code/media/how-to-play-sounds-using-avaudioplayer
var bombSoundEffect: AVAudioPlayer?
let path = Bundle.main.path(forResource: "example.mp3", ofType:nil)!
let url = URL(fileURLWithPath: path)
do {
bombSoundEffect = try AVAudioPlayer(contentsOf: url)
bombSoundEffect?.play()
} catch {
// couldn't load file :(
}
//If you want to stop the sound, you should use its stop() method.
// ---------- System Sounds ----------
//https://stackoverflow.com/a/31126202
//https://github.com/TUNER88/iOSSystemSoundsLibrary
//https://developer.apple.com/documentation/audiotoolbox/audio_services#//apple_ref/c/func/AudioServicesPlaySystemSound
// create a sound ID, in this case its the tweet sound.
let systemSoundID: SystemSoundID = 1016
// to play sound
AudioServicesPlaySystemSound (systemSoundID)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment