views:

157

answers:

3

Apple lists (http://developer.apple.com/samplecode/AudioQueueTest/listing1.html) as a quick demonstration of playing an audio file.

Is there a way to play an audio file with many less lines of code?

+1  A: 

Yes, for iPhone SDK 2.2 there is AVAudioPlayer.

catlan
A: 

Look at the SysSound sample code.

David Dunham
A: 

Here's a quick and dirty way to do sounds. It's suitable for playing short sounds with no control over duration, volume or precise timing. With those disclaimers, here we go.

In the view controller that will play the sound, import this framework:

#import <AudioToolbox/AudioToolbox.h>

Then manually add the AudioToolbox framework to your project with: Control-click on Frameworks Group --> Add... --> Existing Frameworks... --> AudioToolbox.framework

Next, add 'someSound.caf' to your project Resources folder, obviously use your actual filename.

Inside the method that plays your sound, add:

SystemSoundID theSound;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"someSound" ofType:@"caf"]], &theSound);
AudioServicesPlaySystemSound(theSound);   

I use this method for prototyping, not for production code, because of it's many limitations.

willc2