Skip to content

Instantly share code, notes, and snippets.

@carlynorama
Created September 3, 2020 19:39
Embed
What would you like to do?
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