This video is the second (and final) part of a video tutorial which demonstrates my method for playing caf files with the iPhone SDK’s AVAudioPlayer. Part 2 of 2.
This video demonstrates my method for playing caf files with the iPhone SDK’s AVAudioPlayer. Part 1 of 2.
This is a video tutorial version of an earlier post. This video demonstrates my method for creating caf files for use with the iPhone SDK’s AVAudioPlayer.
I think I may have purchased the coolest looking HD enclosure around for my Time Machine backup drive.

Macally HD enclosure
I’ve got to admit that I bought this mostly on looks, however it’s got a ton of connections (Firewire 400/800, eSata, USB ). Unfortunately, since I am running a 2008 mac mini, I can’t utilize the eSata or Firewire 800, but I have it available for the next machine I buy. BTW, inside I am running a WD Caviar Green 1TB drive.
After updating my iPhone to OS 3.0.1 I discovered that I could not run code on it. Apple’s advisory PDF instructs people to use the following command in the terminal (assuming xcode is installed to the typical location).
ln -s /Developer/Platforms/iPhoneOS.platform/DeviceSupport/3.0\ \(7A341\) /Developer/Platforms/iPhoneOS.platform/DeviceSupport/3.0.1
It worked for me
I’m probably not the only one to learn this the hard way, but simply changing the extension of a file from “aiff” to “caf” does not actually produce a proper “caf” file. You’ll find numerous posts claiming that you can do such a thing, but trust me, you’ll leak like a sieve. Sure, it’ll play just fine, but it’ll create a memory leak when playing said file with the AVAudioPlayer in your iPhone project on both the hardware and the simulator.
My new leak-free process for creating sounds is as such:
1. I typically record through garageband (because it is free), and then edit the sound in audacity (also free). I export the sound as a wav file from each app.
2. Convert the wav to a caf by opening the terminal and typing:
afconvert -f caff -d LEI16@11025 infile.wav outfile.caf
and adjusting the value after LEI16@ up or down to balance sound quality and file size.
3. I then drag the “caf” files produced from the step above to the appropriate group/folder in my xcode project with the “Copy items into destination group’s folder” option checked (just my personal preference on copying the files).
Really, only step 2 is worth noting.
A simple class whose purpose was to play sounds might be defined in the following manner:
@interface AVSoundPlayer : NSObject <AVAudioPlayerDelegate> { AVAudioPlayer *msoundPlayer; } @property (nonatomic, retain) AVAudioPlayer *msoundPlayer; -(id)initWithCafFile: (NSString *)inString; -(void) playNum:(int)num; @end
Code to implement the class defined above would look like the following:
@implementation AVSoundPlayer
@synthesize msoundPlayer;
-(id)initWithCafFile: (NSString *)fileName{
if (self = [super init]){
NSBundle *mainBundle = [NSBundle mainBundle];
NSError *error;
NSURL *sURL = [NSURL fileURLWithPath:[mainBundle
pathForResource:fileName ofType:@"caf"]];
self.msoundPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:sURL error:&error];
if (!self.msoundPlayer) {
NSLog(@"Sound player problem: %@", [error localizedDescription]);
}
}
return self;
}
-(void) playNum:(int)num{
self.msoundPlayer.numberOfLoops = num;
[self.msoundPlayer prepareToPlay];
AVAudioPlayer *tmpPlayer = self.msoundPlayer;
[tmpPlayer play];
}
- (void)dealloc {
[self.msoundPlayer release];
[super dealloc];
}
@end