Skip to content

Adding Short Sound Clips to iPhone/iPad Apps

by dkberktas on August 24th, 2010

There are lots of ways of playing/recording audio in your iPhone/iPad app, and probably this is why it took me a while to decide which one to chose,

Here are the alternatives (List of Audio APIs) –

  • System Sound API — very short – limited file type – no control
  • AVAudioPlayer class — objC
  • Audio Toolbox — recording – streaming – controls
  • Audio Units — audio processing
  • OpenAL — 3D positional sound (Yes, just like OpenGL)

Since what I need is just to play a short audio clip w/out any looping or any other control, I choose System Audio API.

As always, Google is the first address to ask howto use System Audio AP, How To Play Audio With The iPhone SDK is a step by step guide, but it takes the starting point from a sample code project from Apple, which is no longer available.

Another one is the sample code project SysSound which contains an iOS4 specific feature so that it will not work on iPad.

So how to play an audio clip w/out hassling that much?

Here is the code snippet : (don’t forget to add the AudioToolbox framework)
#include
….

-(void)playSound
{
	SystemSoundID shortSound;
	NSURL* audioFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
				pathForResource:[NSString stringWithFormat:@"filename"] ofType:@"wav"]];
	AudioServicesCreateSystemSoundID((CFURLRef)audioFile, &shortSound); 

	//playing it
	AudioServicesPlaySystemSound(shortSound);

	//cleanup
	AudioServicesDisposeSystemSoundID(shortSound);

}

From → english, ipad, iphone, tech

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS