How to Play a Short Sound in iPhone Code

Ray Gun

by mattjdrake on August 17, 2009

Today I am going to turn my iPhone into a Ray Gun by having it play a short laser sound. You probably have seen the many gun, laser and farting apps available on the Apple App Store. Most of these apps simply play a short sound along with some fancy graphics.

Here is how you can play a short sound:

Here is the a summary of the steps I took in the video:

Create a View Based iPhone Application

First, create a view based iPhone application in XCode and add a button to your view that will play the sound. If you need a refresher on how to use Interface Builder to hook up controls check out my article on adding a slider control.

Add the Audio Toolbox framework

Drag in the Audio Toolbox framework into your frameworks group in XCode. Here is a video on how to quickly add frameworks in XCode if you need it: Quick Tip: Adding Frameworks Painlessly in XCode. Remember that the framework will be all one word with the .framework extension, AudioToolbox.framework.

Add a short wav file to your project

I used the website Joe’s Original Wave Files for this demo and found a nice laser sound to use. You can also buy the rights to use sounds in apps that you want to sell from websites like iStockPhoto.

Use Audio Services to play the sound

To keep things simple I put all the code that you need to play the sound in an IBAction called “shoot”.

-(IBAction) shoot{
	//Get the filename of the sound file:
	NSString *path = [NSString stringWithFormat:@"%@%@",
					  [[NSBundle mainBundle] resourcePath],
					  @"/jad0007a.wav"];

	//declare a system sound id
	SystemSoundID soundID;

	//Get a URL for the sound file
	NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];

	//Use audio sevices to create the sound
	AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);

	//Use audio services to play the sound
	AudioServicesPlaySystemSound(soundID);
}

That is it – pretty simple way to add some cool effects to your app.

Learn How To Make Your Own iPhone, iPad iOS apps!

Buy Now

If you are completely new to iPhone programming and want to start to developing iPhone apps this ebook will give you everything you need to get started today.

You Get the 5 Essential Things You Need to Make iPhone Apps

  • C Procedural Programming
  • Objective-C Object Oriented Programming
  • Cocoa-Touch Model-View-Controller Design Pattern
  • Architecture of Real World iPhone Apps
  • 12 Step App Development Formula

This ebook is much different than the other books out there: it really is a *system* that includes ONLY what you need to know to make iPhone applications. Not only that, but you will get some real insight on how to make a product and how to set up your code in the best way the first time.

Plus, you will get plenty of bonuses including exclusive source code projects. For more information, head on over to the website for the ebook:

Head over to How To Make An iPhone App eBook Website website to find out how to get Matt’s eBook and the bonus source code.

Please share this if you like it!
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • FriendFeed
  • LinkedIn
  • MySpace
  • Ping.fm
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati
  • Tumblr
  • Yahoo! Bookmarks

{ 3 trackbacks }

iPhoneKicks.com
August 18, 2009 at 1:18 pm
How to Play a Short Sound in iPhone Code | IPhoneMate
August 18, 2009 at 8:46 pm
iPhone SDK Tutorial: Play a short sound in iphone sdk | Iphone Development Exchange
September 27, 2009 at 10:54 am

{ 4 comments }

1 lrniphoneNo Gravatar August 21, 2009 at 12:41 pm

This is awesome, exactly what I trying to learn. However I’m so new at this. The video starts with some code already inputed, I tried reading the other articles in hopes to figure out how to start, but getting confused. Can you help and post the whole code. I would really appreciate it.

2 mattjdrakeNo Gravatar August 21, 2009 at 1:05 pm

Sure thing – I just emailed you the code (the email you used to register for the comment).

3 lrniphoneNo Gravatar August 21, 2009 at 2:32 pm

Great site and look forward to learning more.

4 techreviewcastNo Gravatar August 28, 2009 at 9:12 pm

Hey, great tutorial. Very well done! Do you think you can send me the code so I can further understand it? That would be very helpful. Thanks again!

Comments on this entry are closed.

Previous post:

Next post: