How to add custom sound effects to iPhone app?

How to add custom sound effects to iPhone app?

Here is simple code snippet (class) that enables you to add custom sound effects to iPhone application. As you can see procedure is fairly simple.

I always like to prepare app for future changing, for example, if one day I change my mind and want to change existing clicking sound with new one I don’t have to change it everywhere I have create it, I just change value of effect string instance that is implemented in custom made Sound class in static method + (void) soundEffect:(int)soundNumber.

You can see here that I used static method so I don’t have to create instance of that class just to play a sound. When you want to call sound effect from another class just add this line of code [SoundAlert soundEffect:1]; to play for example an click sound effect.  I recommend you to put this call in viewWillDisappear: method so that sound triggers every time when current view exits.

Now finnaly if I want this class to work without errors I must add sound files. Isn’t that logical or what? I am using .aif  audio file so I must be sure to set type string instance value to @”aif” and also set the name of audio file with same name as value of effect string instance. Here is Sound class.


#import <foundation /Foundation.h>
#import <audiotoolbox /AudioToolbox.h>
@interface Sound : NSObject {
+ (void) soundEffect:(int)soundNumber;


#import "Sound.h"
@implementation Sound
+ (void) soundEffect:(int)soundNumber {
NSString *effect;
NSString *type;
if (soundNumber == 0) {
effect = @"intro";
type = @"wav";
else if (soundNumber == 1) {
effect = @"click";
type = @"aif";
else if (soundNumber == 2) {
effect = @"error";
type = @"aif";
NSString *value = [[NSUserDefaults standardUserDefaults] stringForKey:@"sound"];
if ([value compare:@"ON"] == NSOrderedSame) {
SystemSoundID soundID;
NSString *path = [[NSBundle mainBundle] pathForResource:effect ofType:type];
NSURL *url = [NSURL fileURLWithPath:path];
AudioServicesCreateSystemSoundID ((CFURLRef)url, &soundID);
- (void)dealloc {
[super dealloc];

I have also added switch somewhere in my costume made settings menu that enables user to turn OFF or ON all custom used sounds in app. Here we are checking in which state is switch and if it is on ON position sound will play. You can see how to use NSUserDefaults to save state of switch in my previously post. That’s all!

You made it all the way down here so you must have enjoyed this post! You may also like:

Apple push notifications from Magento Darko Goles
Darko Goles, | 5

Apple push notifications from Magento

Magento API v2 SOAP demystified Darko Goles
Darko Goles, | 39

Magento API v2 SOAP demystified

Develop your own Magento mobile application Ivica Tadic
Ivica Tadic, | 50

Develop your own Magento mobile application


  1. @Sekati: Did you include the AudioToolbox framework in your project?

    @Ivan: Isn’t it necessary to call the AudioXXXDispose method after using the sound?

    Thanks. 🙂

  2. Maybe Im missing something but your class produces several errors in xcode 4:

    24. SystemSoundID soundID; // ‘SystemSoundID’ undeclared & soundID undeclared

  3. This helped clarify an example I was looking at in the Apress “Beginning iPhone Development” book.

    It might be obvious to most, but line 28 doesn’t need 3 URL encoded ampersands! one regular ampersand is all that’s required.


  4. This seems like the info that I’m looking for. What I was trying to figure out is how to add a sound effect when an icon of an APP is pushed on the home screen.

    What actual file do i edit to add the above strings,and also where do i actually upload the audio files. Also I wanted to use a *.caf audio file (like that of the system sounds) to be played when the icon is pushed.

    Any ideas?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.