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)

[sourcecode language=’cpp’]

    SystemSoundID bell;
    AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@”bell” ofType:@”wav”]], &bell);
    AudioServicesPlaySystemSound (bell);

[/sourcecode]

7 thoughts on “Play a short sound in your iPhone application the RIGHT WAY”

  1. > Note 2: Retain the SystemSoundID as an instance if you
    > plan on using it multiple times

    How (and why) would you do that?

    Thanks.

  2. If this is the *right* way to do it, how come it creates warnings at build time?

    warning: passing argument 1 of ‘AudioServicesPlaySystemSound’ makes integer from pointer without a cast

    : warning: passing argument 2 of ‘AudioServicesCreateSystemSoundID’ from incompatible pointer type

  3. My thanks to you for having posted “Play a short sound in your iPhone application the RIGHT WAY! I’ve spent a day and half trying to puzzle it out, but I copies your code AND BINGO IT WORKED!!

    Thanks again, Art

Leave a Reply

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