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:
- Command-click on the Classes folder.
- Select Add > Existing Files
- Navigate to the JigSaw sample project and select the AudioSupport directory. Click Add.
- Check the “Copy items” checkbox. Click Add.
- Now you should have a new folder called AudioSupport under Classes in your project.
You also need to add the AudioToolbox framework
- Command-click on the Linked Frameworks folder inside the Frameworks folder in your Xcode project.
- Select Add > Existing Frameworks
- 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
- 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.
- Command-click on the Resources folder in your Xcode project.
- Select Add > Existing Files
- Navigate to the Sounds directory in the JigSaw project. Select one of the sound files. (They have the .caf filename extension.)
- Click Add.
- 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"]; ;
“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 ; just after ; 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 { ; [super dealloc]; } - (void)viewDidAppear:(BOOL)animated { ; }
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.
August 18th, 2008 at 11:30
Please post a link to this sample app as Apple seem to have taken it down of their site. Thanks!
August 21st, 2008 at 14:23
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
August 22nd, 2008 at 10:46
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.
September 30th, 2008 at 19:59
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.
November 2nd, 2008 at 21:40
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.
January 7th, 2009 at 09:14
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).
February 4th, 2009 at 21:06
can you provide these files AudioFX.h/AudioFx.m Audio_Internal.h they are no more available online
February 6th, 2009 at 10:03
@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.
February 9th, 2009 at 05:44
looking for code for multiple audio files on one page
anyone know of any such -SIMPLE code – i’m a newbie
-mike
July 21st, 2009 at 06:19
The Audio_Internal.h / .m are available on Google …
September 3rd, 2009 at 00:09
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.
February 17th, 2010 at 17:04
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
November 29th, 2011 at 05:28
Great tutorial.. any one know how to play wma files in Iphone applications????
January 16th, 2012 at 01:32
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
February 28th, 2012 at 08:57
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.