How to play audio with the iPhone SDK - iPhone

Here is a simple and concise way to load and play any sound file that the iPhone can play.
You’ll need to include the AVFoundation framework.
#import

The following code also assumes that you have the following AVAudioPlayer defined.
AVAudioPlayer *player;

Here is the code to load and play the file.
NSString *soundFilePath =
[[NSBundle mainBundle] pathForResource: @"Sound" ofType: @"mp3"];

NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];

AVAudioPlayer *newPlayer =
[[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];
[fileURL release];

self.player = newPlayer;
[newPlayer release];

[self.player prepareToPlay];
[self.player setDelegate: self];

if (!self.player.playing) {
[self.player play];
}

iPhone Development