Post Pic

How To Play Sounds In Your iPhone

This tutorial is for the people who want to develop an application that can run a sound in background to the application. iPhone SDK 2.2 has provided AVFoundation framework to do this. It contains very simple APIs for play, pause, resume, control volume, forward, rewind. This tutorial will take you step by step to create a very simple application which can play an mp3 sound. At the end some additional properties and functions are briefly described.

Here is a step by step guide, if you have any questions, comment below. Good luck and enjoy!

Create Project:

1) Open XCode.

2) File -> New Project…

3) Select Application from left panel under iPhone OS as specified in the picture below.

4) Select View-Based-Application from right pane as specified in the picture below.

5) Click Choose…

image1

6) File Dialog will open as specified in picture below. Select your location and enter project name as SoundPlay in the text box in front of Save As.

7) Click Save. The project is ready to start work on it.

Image-2

Place Sound file:

8) Drag an mp3 file named sound1.mp3 from your file System and drop it in Resources of the project at left pan as shown in the picture.

image3

9) A dialog will appear as shown in the picture below. Check “Copy items into destinations group’s folder (if needed)”.

10) Click Add.

11) You can see sound1.mp3 under Resources as in picture below.

image4

Add required Framework:

12) Right click on FrameWorks and click Add->Existing Frameworks… as shown in picture below.

image5

13) A dialog will open.

14) Select AVFoundation.framework as shown in picture below.

15) Click Add.

16) Another dialog will appear. Click Add.

17) Now you can see AVFoundation.framework under Frameworks of your project.

image6

Add Code:

18) Open SondPlayViewController.m from your Project.

19) Write following code of import statements at the top of the file as shown in picture below.

#import

20) Uncomment the -

(void)viewDidLoad and add following code in that as sown in picture below.
NSError *error;
NSString *newAudioFile = [[NSBundlemainBundle] pathForResource:@”sound1″ofType:@”mp3″];
AVAudioPlayer *av = [[AVAudioPlayeralloc] initWithContentsOfURL:[NSURLfileURLWithPath:newAudioFile] error:NULL];
if(error) {
}
[av play];

image7

21) Now You are ready to run and listen the sound. Click on Build and Go from the top. You will be shown a blank view and a sound will play. Enjoy the code.

AVAudioPlayer Additional Features:

AVAudioPlayer has following important commonly used properties and functions to control the sound.

volume: Property to set or get the volume of the playing sound. Value can range from 0.0 to 1.0.

duration: Property to get the time duration of the sound.

currentTime: Property to set ot get the current location of the playing sound. This can be used to forward and rewind a sound.

playing: Boolean variable to tell whether sound in playing or not.

volume: Property to get or set the volume of the playing sound. Value can range from 0.0 to 1.0.

pause: Function to pause the playing song.

Play: Function to play the song from the same location where pause is called.

Related Posts

  • How To Add A Basic Webview To A App


  • 10 Responses

    06.20.09

    Thanks for sharing this tutorial. Helped me a lot!

    06.20.09

    I really appreciate this tutorial. I have made several attempts to follow this tutorial verbatim, and always have the same results.

    error: ‘NSBundlemainBundle’ undeclared (first use in this function)
    error: syntax error before’]’ token
    warning: unused variable ‘newAudioFile’

    error: ‘AVAudioPlayeralloc’ undeclared (first use in this function)
    error: syntax error before ‘]’ token
    warning: unused variable ‘av’

    I am running simulator 2.2.1 and Xcode 3.1.2

    Any help would be greatly appreciated.

    06.20.09

    @Erik – sorry for the delayed response. Anyways, this tutorial was actually an older tutorial and only works in 2.2 and not 3.0. That’s why you are having that error.

    06.20.09

    is there a tutorial for doing this in 3.0 ?

    06.20.09

    What I’ll do is put one in the making and post a comment here to see the updated tutorial. Thanks for your questions and feedback. If you have any other tutorials you would like, please let me know.

    06.20.09

    Can you post the link for the tut in 3.0?

    06.20.09

    @Rushil – I put the “How to Play Sounds in Your iPhone 3.0″ in the tutorial cue. Check back soon! Thanks.

    06.20.09

    @Erik just put the – before the () like this

    - (void)viewDidLoad

    that will take care of your errors, if you need any more help email me @ spicysquirrel@gmail.com

    Enjoy! And Keep coding

    06.20.09

    Typically the file extensions that are allowed are:

    - .caf
    - .m4a
    - .wav
    - .mp3

    In my opinion I find the first 3 easier to work with rather than the popular .mp3 format. For some reason there are audio clips in .mp3 format which don’t seem to work. Luckily there are so many .mp3 to .wav converters around which you can use. Or the easiest way will be to use iTunes to convert to AAC format which will assign a .m4a extension for the audio clip.

    06.20.09

    Hi there,

    Thank you for the tutorial, how do I take this further and have mutiple buttons that play specific sounds?

    I can’t find any help anywere for this particular question?

    Leave Your Response

    * Name, Email, Comment are Required
    Subscribe via RSS

    Receive the latest iPhone app tutorials and resources delivered to you. Free.

    Latest Tweet



    Follow us on Twitter

    Write for Us

    Get your name in front of the masses by writing for iPhone App Tuts. We will share a plug about you and give links to your site and Twitter page.

    Write for Us

    We need your feedback

    Would you pay a small monthly subscription for consistent iPhone tutorials?

    View Results

    Loading ... Loading ...