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.

Sound.h

#import <foundation /Foundation.h>
#import <audiotoolbox /AudioToolbox.h>

@interface Sound : NSObject {

}

+ (void) soundEffect:(int)soundNumber;

@end

Sound.m

#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);

AudioServicesPlaySystemSound(soundID);

}
}

- (void)dealloc {
[super dealloc];
}

@end

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!

8
Top

Enjoyed this post?

Subscribe to our RSS Feed, Follow us on Twitter and spread it to your friends!

Author

Ivan Kalaica

Senior OSX and iOS SDK Developer

At Inchoo and Surgeworks Ivan Kalaica is a Senior OSX and iOS SDK developer.

Other posts from this author

Discussion 8 Comments

Add Comment
  1. G S K

    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?

  2. 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.

    Viv

  3. Ivan Kalaica

    Yeah, common copy&paste issue… :-)

  4. Jack

    Why not use a switch to determine the soundNumber? Since we are only validating the one value…

  5. Harry

    Thank you!

  6. sekati

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

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

  7. @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. :)

  8. EvanB

    Perfect!

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top