Skip to content

Instantly share code, notes, and snippets.

@dnedrow
Last active December 7, 2023 21:27
Show Gist options
  • Select an option

  • Save dnedrow/2e7d7e48f30f8ffa2497eae3789869da to your computer and use it in GitHub Desktop.

Select an option

Save dnedrow/2e7d7e48f30f8ffa2497eae3789869da to your computer and use it in GitHub Desktop.
Simple mechanism for playing arbitrary sound files in Swift
import Foundation
import AudioToolbox
import Foundation
import AudioToolbox
// This provides additions to SystemSoundID
extension SystemSoundID {
/// Play a sound
/// This can be handy when attached to notify on breaking constraints.
/// Must import AudioToolbox in class attempting to call this method.
///
/// -parameter fileName: The name of the file.
/// -parameter fileExtension: The file's extension.
///
static func playFileNamed(fileName: String, withExtension fileExtension: String) {
var sound: SystemSoundID = 0
if let soundURL = Bundle.main.url(forResource: fileName, withExtension: fileExtension) {
AudioServicesCreateSystemSoundID(soundURL as CFURL, &sound)
AudioServicesPlaySystemSound(sound)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment