Posts tagged: AudioServicesPlaySystemSound

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

WordPress Themes