How do I auto play Audio files? - iphone

Heres what I have:
-(void)awakeFromNib {
NSString *path = [[NSBundle mainBundle] pathForResource:***AUDIOFILE*** ofType:#"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]
error:NULL];
[theAudio play];
The coding is fine, except I don't know how to call out my resource for the sound to play or whatever.

If your Audio file is inside your project then you just need to give Filename without extension in place of *AUDIOFILE*.
It is good practice to use [theAudio prepareToPlay] method for AVAudioPlayer.

Related

pathForResource dynamic input

I am trying to write code with dynamic input for pathForResource but I get null value for the directory listing. the files I am using are wav files. Here is the code. I have a.wav file in the project folder. What is wrong here? how can I make this work? Please help.
int alphanum = 97;
NSString *alphastring = [NSString stringWithFormat:#"%i",alphanum];
NSString *path = [[NSBundle mainBundle] pathForResource:alphastring ofType:#"wav"];
AVAudioPlayer *theplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theplayer play];

Problem with AVAudioPlayer ? Creating delay

While using AVAudioPlayer .... Sound not playing at instant if i click button... and in that class i have one NSTimer which keep changing image so for that purpose i have to ring sound.
But If i will use this AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);... i am not able to hear sound in device.. cause my file is in mp3 format.
AVAudioPlayer Code:
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/slotmachine.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
So anyone can tell me why i am not able to use AVAudioPlayer so smoothly like System Sound does.. what is the appropriate way to integrate ?
Call [audioPlayer prepareToPlay] well before you want to actually play the audio.

iPhone Load Audio File Question

I am currently loading an audio file inside of a button. I am assuming there is a more efficient way to load this file instead of every time the button is pressed. Where should this be loaded? Globally, inside viewdidload. I'm loading it like the code below.
NSString *path = [[NSBundle mainBundle] pathForResource:#"click" ofType:#"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
viewDidLoad is a good idea -- will let you free up the memory in viewDidUnload too in case you are short on memory. You could also implement getters and setters in the code below by synthesizing theAudio and using self.theAudio
#interface myClass {
AVAudioPlayer *theAudio;
}
#implementation myClass {
- (void) viewDidLoad {
NSString *path = [[NSBundle mainBundle] pathForResource:#"click" ofType:#"mp3"];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
}
- (void) viewDidUnload {
[theAudio release];
}
}

Why isnt the sound playing, no errors in the code?

The sound clip is 4 seconds, wav file and 700kb in size.
Here is the code which I have used, I have linked everything up included the framework of AVAudioPlayer etc.
- (IBAction)twoSound:(id)sender; {
NSString *path = [[NSBundle mainBundle] pathForResource:#"2b" ofType:#"wav"];
if (theAudio) [theAudio release];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
When I tap the button it makes a small "bump" noise, but this isn't the noise it's supposed to do.
Thanks
Use AAC or CAF instead; Preferred Audio Formats in iOS

I can not play mp3 file using AVAudioPlayer

I want to play mp3 file placed in resources as code is given below,
NSString *st=#"alarm_1.mp3";
NSLog(st);
NSURL* musicFile = [NSURL fileURLWithPath:st];
AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
//AVAudioPlayer *click1=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL URLWithString:#"alarm_1.mp3"]];
[click prepareToPlay];
[click play];
[click release];
but unfortunately i cant hear the sound
please help
I don't think your file name is right. Try this syntax:
NSURL *clickURL = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/alarm_1.mp3", [[NSBundle mainBundle] resourcePath]]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:clickURL error:nil];
I had a similar problem due to ARC. Instead of defining the AVAudioPlayer in the same method you are using it, you should should have an instance variable somewhere else, such as the UIViewController. This way ARC doesn't auto release this object and audio can play.
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/alarm_1.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
This is answer