Play a short sound in your iPhone application the RIGHT WAY
There are LOTS of answers to this question out there on the internet, many of them old and no longer applicable. LOTS of them use CF instead of NS libraries and many of them are LEAKY! So after some research, and some help from the guys at #iphonedev (Thanks august, millenomi, sgtblue52) I think I finally landed on the right way to do it.
Note 1: Works for AIF or WAV files, make sure they are properly added to your project.
Note 2: Retain the SystemSoundID as an instance if you plan on using it multiple times
Note 3: In my example, I added the file “bell.wav” to my project (and made sure it added it to the folder as well)
SystemSoundID bell; AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"bell" ofType:@"wav"]], &bell); AudioServicesPlaySystemSound (bell);
2 Comments
Other Links to this Post
RSS feed for comments on this post. TrackBack URI
By Susan, July 26, 2010 @ 5:13 pm
> Note 2: Retain the SystemSoundID as an instance if you
> plan on using it multiple times
How (and why) would you do that?
Thanks.
By Kevin Lohman, August 3, 2010 @ 8:03 pm
Susan, you can accomplish this by making SystemSoundID bell an instance variable. Otherwise you are making more calls to Audio Services then needed (wasteful).