iPhone SDK Play sound using AudioServicesPlaySystemSound Method

March 22, 2009 § 13 Comments

Getting audio to play is one of the most tricky businesses in iPhone SDK development. AudioServicesCreateSystemSoundID is designed to play 30 sec or less audio file using AudioToolbox framework.

You would have to add this line to include AudioToolbox framework on your MainViewController.m source file.

#import <AudioToolbox/AudioToolbox.h>

I would like to show the easiest way to play the sound without using memory or specific the exact audio file path. You can place the audio file according to your preferences, preferably  under Resources folder. This example uses a Interface Builder button UIButton soundButton to trigger the sound. It will locate the alert.wav and use AudioServicesPlaySystemSound to play the sound.

– (IBAction)soundButton:(id)sender {

NSString *soundPath = [[NSBundle mainBundle] pathForResource:@”alert” ofType:@”wav”];

SystemSoundID soundID;

AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);

AudioServicesPlaySystemSound (soundID);

[soundPath release];



§ 13 Responses to iPhone SDK Play sound using AudioServicesPlaySystemSound Method

  • Dipen says:


    This is a wonderful post from you…
    Thanks very much

  • aryan says:

    thanks a lot
    Finally no more leaking on my code.
    But i got a question.
    Why would u release the “soundPath” when u don’t allocate it. I mean i know it’s the correct way, I am just trying to understand


  • Ben says:

    The posted way works pretty good but if you want to play longer audio files or mp3 you will need to include AVFoundation. I posted the code necessary to play the music on my homepage:
    Just goto http://www.bhuio.com goto code, follow the steps and copy the source code. If you have any questions contact me mail@bhuio.com.

    Good luck, Ben.

  • Michael says:

    I actually tried exactly this code, and cannot get the sound to play on either the simulator or an iPod touch device. Perhaps there’s a system/simulator setting that needs to be in place to get it working? Help?

    • svwebbuilder says:

      It could be your audio source, format and type. QA your audio using workable code to narrow down problem areas.

  • Carol says:

    Your line does *NOT* add the framework:

    > You would have to add this line to include AudioToolbox
    > framework on your MainViewController.m source file.
    > #import

    Do you even know what “frameworks” are????

  • Bonnie says:

    > [soundPath release];

    Why are you releasing something that you have NEVER alloc’ed????

    (My students are learning all the worst errors, from this site.)

  • Lance Drake says:

    The iPhone SDK docs suggest that the iPhone is happiest with 16-bit Little-Endian Linear PCM audio in a ‘caff’ data format. One way you can create a ‘.caf’ file from your audio is to use ‘afconvert’ from the command line in a terminal window.

    For details about afconvert – type “afconvert -h > ~/Desktop/afconvert.txt” to create a text file on your desktop with all of the details.

    Once example I have used is:
    $ cd [path of my original audio file]
    $ afconvert -v -f ‘caff’ -d ‘LEI16’ mybeep.aif mybeep.caf

  • Ulbrich says:

    when I build and rund the code i have this error:

    “_OBJC_CLASS_$_AVAudioPlayer”, referenced from:
    Objc-class-ref-to-AVAudioPlayer in MyappViewController.o
    Symbol(s) not found
    Collect2: Id returned 1 exist stats

    meaby you can help me, I’m new with this and I’m ussing the 4.2 Xcode Simulator

    Sorry for my bad english.
    I hope you can help me

    • svwebbuilder says:

      It’s difficult to troubleshoot without reading your code or viewing xcode. I am guessing you mess up the audio object or id

  • Sandeep says:

    thnx dear

    this is fantastic code

  • Connor says:

    Hi, yup this post is truly fastidious and I have learned lot of
    things from it on the topic of blogging. thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

What’s this?

You are currently reading iPhone SDK Play sound using AudioServicesPlaySystemSound Method at Web Builders.


%d bloggers like this: