Skip to content

Instantly share code, notes, and snippets.

@NikhilManapure
Created January 10, 2017 05:23
Show Gist options
  • Save NikhilManapure/75b79d98195131f1a3a8bb756c494fa3 to your computer and use it in GitHub Desktop.
Save NikhilManapure/75b79d98195131f1a3a8bb756c494fa3 to your computer and use it in GitHub Desktop.
func play(sound: String, ofType type: SoundExtension) {
if let soundID = notificationSoundLookupTable[sound] {
AudioServicesPlaySystemSound(soundID)
} else {
if let soundURL : CFURL = Bundle.main.url(forResource: sound, withExtension: type.rawValue) as CFURL? {
var soundID : SystemSoundID = 0
let osStatus : OSStatus = AudioServicesCreateSystemSoundID(soundURL, &soundID)
if osStatus == kAudioServicesNoError {
AudioServicesPlaySystemSound(soundID);
notificationSoundLookupTable[sound] = (soundID)
}else{
// This happens in exceptional cases
// Handle it with no sound or retry
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment