System Sounds in Cocoa

smallnote

By far the simplest way to use a system sound in your application is to use NSBeep() in your code. You might want to use it to alert a user of your program that their their actions, such as pressing certain keys, have no purpose at the moment:

- (void)keyDown:(NSEvent *)theEvent {
    NSString *keyChar = [theEvent charactersIgnoringModifiers];
    if ([keyChar isEqualToString:@"a"]) {
        [self yourMethod];
    }
    else {
        NSBeep();
    }
}

NSBeep() plays the alert sound the user has selected in System Preferences. If the user chooses a different alert sound, NSBeep() then plays the new sound. If you want to use one of the other alert sounds (which are found in /System/Library/Sounds), you can play it by using an instance of the NSSound class:

- (void)playSystemSound {
	NSSound *systemSound = [NSSound soundNamed:@"Glass"];
	[systemSound play];
}

Notice that you don’t need to know where the system sounds are for this to work and that you don’t include the sound’s file extension in the method that loads the sound. While it’s pretty unlikely that the end user has removed the sound you want to use in your program, the system sounds can all be altered, removed, or added to at will, so to be prepared for an adventurous user you might want to include the sound in your application’s bundle. The code would remain unchanged.

Here’s an idea you could use in a game. Every time a player makes a mistake the game plays an alert sound and the sound changes after each mistake. The following method retrieves the names of the alert sounds, whether they have been altered or not, and plays the next one every time the method is called again, eventually cycling through them all if the method is called enough times. When they have all been played the method starts at the beginning of the array again:

- (void)selectAndPlay {
    NSArray *directoryContents = [[NSFileManager defaultManager]
        directoryContentsAtPath:@"/System/Library/Sounds"];
    int arrayCount = [directoryContents count];
    if (soundIndex < arrayCount) {
// int soundIndex has already been declared and intialized to 0
	NSString *soundName = [directoryContents objectAtIndex:
            soundIndex];
	soundName = [soundName stringByDeletingPathExtension];
	NSSound *systemSound = [NSSound soundNamed:soundName];
	[systemSound play];
	soundIndex += 1;
    }
    else {
	soundIndex = 0;
	NSString *soundName = [directoryContents objectAtIndex:
            soundIndex];
	soundName = [soundName stringByDeletingPathExtension];
	NSSound *systemSound = [NSSound soundNamed:soundName];
	[systemSound play];
	soundIndex += 1;
    }
}

If you're curious, you can make your own system sounds as long as you have a way to produce an AIFF file. Apple states that the file extension must be .aiff, although .aif works as well. You can use iTunes to convert a sound file of a different type (such as WAV or MP3) to an AIFF file by using the AIFF encoder in the import settings (in Preferences) and using Create AIFF Version in the Advanced menu. If nothing is selected in iTunes you can hold down the option key as you click on the menu item and convert any sound file on your computer (assuming it's the right type of file, of course - I don't have a comprehensive list for you yet). Once you've created your file you just need to move or paste it into the /System/Library/Sounds folder. You will be prompted for the root password.

Leave a Reply

You must be logged in to post a comment.