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
Related
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.
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.
I have a rectangle button, when the user presses it once. the sound plays
But I want it so when the user double taps the button, the sound continously loops till the user decided to press to stop the sound.
This is the code to play the sound.
- (IBAction)oneSound:(id)sender; {
NSString *path = [[NSBundle mainBundle] pathForResource:#"1" ofType:#"wav"];
if (theAudio) [theAudio release];
NSError *error = nil;
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
if (error)
NSLog(#"%#",[error localizedDescription]);
theAudio.delegate = self;
[theAudio play];
}
Thanks!
Add this line:
theAudio.numberOfLoops = -1;
The negative value makes the player loop forever until it is stopped.
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
AVAudioPlayer *myExampleSound;
NSString *myExamplePath = [[NSBundle mainBundle] pathForResource:#"myaudiofile" ofType:#"caf"];
myExampleSound =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:myExamplePath] error:NULL];
myExampleSound.delegate = self;
[myExampleSound play];
How can I play myExampleSound such that it automatically replay after its end?
I am using the above code.
Anyone please help.
The AVAudioPlayerDelegate has got a method
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
which is called after the sound has finished playing. You have to implement this method and then you are able to play your sound again.