Snippet: Playing a System Sound

If you are using ARC see: “Playing a System Sound (ARC Version)”.

Today I needed to add a simple beep sound to an app. The code snippet below shows all the relevant calls.

// Required import... Also need AudioToolbox.framework
#import <AudioToolbox/AudioToolbox.h>

// ivar 
SystemSoundID mBeep;

// Create the sound ID
NSString* path = [[NSBundle mainBundle] 
                     pathForResource:@"Beep" ofType:@"aiff"];
NSURL* url = [NSURL fileURLWithPath:path];
AudioServicesCreateSystemSoundID((CFURLRef)url, &mBeep);

// Play the sound
AudioServicesPlaySystemSound(mBeep);

// Dispose of the sound
AudioServicesDisposeSystemSoundID(mBeep);

If I had found a page with a snippet like this on it it would have saved me some time. Maybe this page will save you some!

About idz

A professional software engineer dabbling in iOS app development.
This entry was posted in Code Snippets and tagged , , , . Bookmark the permalink.