Learning Cocoa By Example Happy Pi Day!
Mar 10

Playing audio with the iPhone Software Development Kit (SDK) should be easy, right? After all this is an iPod – the music jukebox.

After some fruitless experiments I decided to take a shortcut and use the audio support classes in the JigSaw sample application. Copy the entire AudioSupport directory from the JigSaw project. It should contain the following files:

  • Audio_Internal.h
  • AudioFX.h
  • AudioFX.m

 

You can copy and add the files to your project in Xcode in one fell swoop:

  1. Command-click on the Classes folder.
  2. Select Add > Existing Files
  3. Navigate to the JigSaw sample project and select the AudioSupport directory. Click Add.
  4. Check the “Copy items” checkbox. Click Add.
  5. Now you should have a new folder called AudioSupport under Classes in your project.

 

You also need to add the AudioToolbox framework

  1. Command-click on the Linked Frameworks folder inside the Frameworks folder in your Xcode project.
  2. Select Add > Existing Frameworks
  3. Navigate to the AudioToolbox.framework folder. Click Add. If you can’t immediately find this file, try this location: /Developer/Platforms/AspenSimulator.platform/Developer/SDKs/AspenSimulator1.2.sdk/System/Library/Frameworks
  4. Click Add again.

 

Your project should now build without any errors.

 

Of course you need an audio file to play. The iPhone is capable of playing a large number of audio file formats [link to dev center]. But to start with I recommend that you select a file that is known to work. Let’s go back to the JigSaw sample application again.

  1. Command-click on the Resources folder in your Xcode project.
  2. Select Add > Existing Files
  3. Navigate to the Sounds directory in the JigSaw project. Select one of the sound files. (They have the .caf filename extension.)
  4. Click Add.
  5. Check the “Copy items” checkbox. Click Add.

 

Now we need some code to play the audio file.

 

Add this at the top of your file:

#import "AudioFX.h"

 

Then use this code where you want to play the audio file:

AudioFX *audio = [[AudioFX alloc] initWithPath:@"Completed.caf"];
[audio play];

 

“Completed.caf” is the name of the audio file that you added as a resource to your project.

The above code is simple and to the point, but it has a few problems. One obvious one is that an AudioFX object is allocated but never released. Adding [audio release]; just after [audio play]; will blow up because the play method is asynchronous: it returns immediately. And releasing the AudioFX object while it’s playing is not very nice.

Another problem is that the code allocates a new AudioFX object each time that audio file is played. If you are going to play the same audio more than once, you should create the AudioFX object once in the init method and use it throughout the life of the app. You can then safely release the object in the dealloc method. This also solves problem number one.

 

Here’s the final code, used in a UIController:

- (id)init
{
  if (self = [super init]) {
    // Initialize your view controller.
    self.title = @"Play Audio"; 

audio = [[AudioFX alloc] initWithPath:@"Completed.caf"];
  }
  return self;
}

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

- (void)viewDidAppear:(BOOL)animated
{
  [audio play];
}

 

UPDATE: This code example is outdated. Please see the SoundEffect class used in the BubbleLevel, GLPaint and Metronome projects instead. The interface is very similar, but instead of initWithPath use initWithContentsOfFile.

written by Nick \\ tags:

15 Responses to “How To Play Audio With The iPhone SDK”

  1. Luiz Says:

    Please post a link to this sample app as Apple seem to have taken it down of their site. Thanks!

  2. Chris Says:

    Hi!
    Can you please provide a link to the JigSaw application or post the source to this example project?

    Apple seems to have taken down the JigSaw example…

    Best Regards
    Chris

  3. Nick Dalton Says:

    As the SDK has evolved Apple has not maintained all the sample code projects. For an example of playing audio see the SoundEffect class used in the sample projects BubbleLevel, GLPaint and Metronome.

  4. Sikosis Says:

    Ah … that would explain why I couldn’t find the jigsaw sample … because it’s been removed.

    But seriously … am I the only one that thinks that is ***WAY TOO MUCH*** code just to play a sound file. Not having a shot at you mate, but Apple need to wake the f–k up.

  5. Josh Says:

    Hi, has anyone tried to play two audio files at the same time? if you are asking why I need this, is to make the fade out of the current song while a new one starts playing. I have tried different things with no success on the phone ( it worked on the simulator). any help would be greatly appreciated.

  6. Will Says:

    Josh – You need to use the Audio Queues portion of the SDK. This has what you need to do it at the same time. I’m learning myself, but I know that from reading the docs.

    Anything less than 30 seconds and is done without overlapping another event, you use a system sound to play a quick clip. For what you want, it sounds like you need to queue the sound(s).

  7. sana Says:

    can you provide these files AudioFX.h/AudioFx.m Audio_Internal.h they are no more available online

  8. Nick Says:

    @sana: You can use the SoundEffect class instead of AudioFX. It does pretty much the same thing and is available in several of Apple’s current sample code projects.

  9. mike Says:

    looking for code for multiple audio files on one page

    anyone know of any such -SIMPLE code – i’m a newbie

    -mike

  10. Raymond Pecoskie Says:

    The Audio_Internal.h / .m are available on Google …

  11. Michel Fortin Says:

    Perhaps will be of interest to some: I’ve made audio engine targeted towards games which simplifies greatly how you can play music and sounds: Asounding.

  12. Jill Taylor Says:

    I have never written to a forum before and am hoping I am doing this right. I would love to receive an email from anyone who knows if any of the voice recorder apps can be played in an iPhone dock. The Voice Memo app on my phone does not play on a dock. Thank you.

    Jill

  13. binoj Says:

    Great tutorial.. any one know how to play wma files in Iphone applications????

  14. Sylvia Neu Says:

    I have fun with, lead to I discovered exactly what I was taking a look for. You have ended my 4 day long hunt! God Bless you man. Have a great day. Bye

  15. Manchester Apps Says:

    An easy to follow introduction to Audio FX / Jigsaw.. thanks.

    I already had set events at which I wanted to play sounds, so plugging in the [audio play] functionality was a doddle.

    Cheers.

    Mike.

Leave a Reply