AppleGrew
AppleGrew

Reputation: 9570

How do I trigger sound alert in OSX using Cocoa?

When Finder finishes copying of files then it triggers a sound alert. How can I do that from my app?

Please note that it is not the same as simply playing a sound. I am from Windows background so I am assuming that OSX allows users to configure notification sound from some central location. So, if the user chooses a different sound for an event then that API should play that new sound. This way I can make my app gel into the system and it will be able to alert the user using sound which the user is familiar with.

Upvotes: 5

Views: 2874

Answers (3)

Shmidt
Shmidt

Reputation: 16664

NSSound(named: "Funk")?.play()

Upvotes: 2

AppleGrew
AppleGrew

Reputation: 9570

Answering my own question.

Update

Additional notes

The system alerts are the ones which user can configure, others like emptying recycle bin, sound made when copying files are not.

NSBeep is the simplest way to trigger the alert sound which notifies the user of an error. Other sounds are available at the following locations in Lion.

  • /System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds
  • For other user interface sounds check the Resources folder under related packages of the core applications. These application packages can be found in /System/Library/CoreServices/.

So, for example if you want to play the move to recycle bin sound then use the following code.

NSSound *systemSound = [[NSSound alloc] initWithContentsOfFile:@"/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/dock/drag to trash.aif" byReference:YES];
if (systemSound) {
    [systemSound play];
}

Caveats

The name and path of the sound files may change at anytime. In fact the location of SystemSounds before Lion was /System/Library/Components/CoreAudio.component/Contents/Resources/SystemSounds.

Upvotes: 6

emkaka
emkaka

Reputation: 121

You should also check this guide http://cocoathings.blogspot.com/2013/01/playing-system-sounds.html

Upvotes: 4

Related Questions