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.