iPhone SDK Play Audio using AVAudioPlayer Method

March 22, 2009 § 6 Comments

This is the latest audio framework Apple added to iPhone OS 2.2. It would not support previous version earlier than OS 2.2.

The advantages are there is no limitation on audio length and  it supports more audio formats. It allows to add more customization to the Audio Player such as volume and no. of loop.

I would like to show the easiest way to get the audio to play.

Add AVFoundation framework and add the following line to MainViewController.h.

#import <AVFoundation/AVFoundation.h>

Add the following line to any method.

AVAudioPlayer *player;

NSString *soundPath = [[NSBundle mainBundle] pathForResource:@”alert” ofType:@”wav”];

player =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: soundPath] error:nil];

player.volume = 1.0;

player.numberOfLoops = 0; // play once

[player prepareToPlay];

[player play];

§ 6 Responses to iPhone SDK Play Audio using AVAudioPlayer Method

  • John meyer says:

    Can you tell me how to make it so I can press a button and the sound is played?

    • svwebbuilder says:

      Probably I can find some spare time after Apple approve my iphone app. You can find snippets on this blog to figure things out.

  • John meyer says:

    And also, is there a way you can send me the actual project file for the code above. That would be a great help. Thanks!

    • svwebbuilder says:

      I haven’t figured out where to host sample project yet. I cover many undocumented issues and under explained areas on this blog. I research a few areas in greater depth where I couldn’t find any developer books and developer forums covering the issues. It is extremely useful for iphone beginner developers. For now I would suggest you to examine SysSound example you can download from Apple iPhone Developer Center. You would have to sign up a free developer account.

      You double click on the SysSound.xcodeproj and it will open up the Xcode project. It contains just enough files to demonstrate how to use a button to play a sound. Use it as a base to understand the project structure.

      Unfortunately my blog is not intended to show tutorial. There are better iphone tutorial blogs that show screen shoot and go into each step. My blog is intended to help developers who get into ugly situations where you can’t find good sources of information.

  • Jim Pickrell says:

    Cool and helpful note. This worked for me.

    Do you have any idea about the relative advantages/disadvantages of using the two different sounds systems, system sounds and av sounds?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading iPhone SDK Play Audio using AVAudioPlayer Method at Web Builders.

meta

%d bloggers like this: