Reply to comment
How to play audio with the iPhone SDK - iPhone
Submitted by Developing on Thu, 03/12/2009 - 11:09Here 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];
}
