Last active
December 7, 2023 21:27
-
-
Save dnedrow/2e7d7e48f30f8ffa2497eae3789869da to your computer and use it in GitHub Desktop.
Simple mechanism for playing arbitrary sound files in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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