Playing short sounds in Objective-C, iPhone SDK.

Playing sounds in iPhone SDK can be realized in many ways. This solution shows how to play short sounds using SystemSoundID class and can be taken into consideration when playing sounds f.e in menu of your application.

Before we start writing code we should add AudioToolbox framework to our project.

Now the code part. First of all we have to import AudioServices.h to our project.

#import <AudioToolbox/AudioServices.h>

Than we can create a container for our sounds. It can be either an array of SystemSoundIDs, NSMutableArray or a single SystemSoundId object.

SystemSoundID sounds[10];

Now we have to initialize our sound. To do so in init or viewDidLoad etc. :

//Path for our sound:
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"my-sound" ofType:@"wav"];
CFURLRef soundURL = (__bridge CFURLRef)[NSURL fileURLWithPath:soundPath];
AudioServicesCreateSystemSoundID(soundURL, &sounds[0]);

To play it:


5 thoughts on “Playing short sounds in Objective-C, iPhone SDK.

Leave a Reply