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.
Add the following line to any method.
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