Skip to content

Instantly share code, notes, and snippets.

@revblaze
Created January 7, 2024 20:58
Show Gist options
  • Save revblaze/2fb4bd604e0184330d7be590b48b721a to your computer and use it in GitHub Desktop.
Save revblaze/2fb4bd604e0184330d7be590b48b721a to your computer and use it in GitHub Desktop.
Play macOS system sounds within your application environment. Working as of Sonoma (macOS 14).
import AVFoundation
struct SoundUtility {
static func play(systemSound: SystemSound) {
if let sound = NSSound(contentsOfFile: systemSound.path, byReference: true) {
sound.play()
} else {
print("Failed to play system sound")
}
}
}
enum SystemSound {
case trash, poof, mount, unmount
var path: String {
switch self {
case .trash: return "/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/dock/drag to trash.aif"
case .poof: return "/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/dock/poof item off dock.aif"
case .mount: return "/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/system/Volume Mount.aif"
case .unmount: return "/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/system/Volume Mount.aif"
}
}
}
@revblaze
Copy link
Author

revblaze commented Jan 7, 2024

Usage

// play trash sound
SoundUtility.play(systemSound: .trash)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment