Looping a sound in Cocoa Touch - iphone

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.

Related

An AVPlayerItem cannot be associated with more than one instance of AVPlayer

I am getting an error when using AVAudioPlayer in Iphone. i got this error sometime not every time. error is :
An AVPlayerItem cannot be associated with more than one instance of AVPlayer
Code which i am using for playing a file :
NSString *soundPath =[[NSBundle mainBundle] pathForResource:#"abc" ofType:#"m4a"];
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
NSError *error;
AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
[theAudio setDelegate:self];
[theAudio setNumberOfLoops:0];
if(theAudio == nil)
{
NSLog([error description]);
}
else
{
[theAudio play];
}
can anyone tell me how can i solve this issue.

The warning sound is not playing in device?

I have an Ipad application in which i am playing a bit of sound after my operation success .it was working fine earlier.i am doing like this `
NSError *error;
NSString *bell = [[NSBundle mainBundle] pathForResource:#"sound" ofType:#"aif"];
AVAudioPlayer *av = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:bell] error:&error];
if(error) {
NSLog(#"%#",&error);
}
[av play];
but now the sound is not playing in ipad device.but in simulator it was working fine?can anybody help me?
Check if file exists:
NSError *error;
NSString *bell = [[NSBundle mainBundle] pathForResource:#"sound" ofType:#"aif"];
if([[NSFileManager defaultManager] fileExistsAtPath:bell]){
AVAudioPlayer *av = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:bell] error:&error];
if(error) {
NSLog(#"%#",&error);
}
[av play];
}
else
{
NSLog(#"File doesnot exist");
}
Note :Check phone is not set to silent mode

how to stop sound on pressing a button

I am Using Following code to play sound
NSString *laughSoundPath = [[NSBundle mainBundle]pathForResource:#"laugh" ofType:#"wav"];
NSURL *laughURL = [[NSURL alloc]initFileURLWithPath:laughSoundPath];
laughTone = [[AVAudioPlayer alloc]initWithContentsOfURL:laughURL error:nil];
[laughTone prepareToPlay];
[laughTone setDelegate:self];
[laughURL release];
-(IBAction)play
{
[laughTone play];
}
i am Using following line to stop sound
-(IBAction)stop
{
[laughTone stop];
}
Problem is.. When i play sound again, its starting from the point where it was stopped.. i mean to say its working as pause.. help me out
-(IBAction)stop
{
[laughTone stop];
[laughTone playAtTime:0];
}
Try resetting the time and then playing, again put the code in your play method:
laughTone.currentTime = 0;
[laughTone play];
Try this:
NSString *Str = [[NSBundle mainBundle] pathForResource:#"Ahhh" ofType:#"wav"];
AVAudioPlayer *audioSound =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:ahhhPath] error:NULL];
-(IBAction)btnStopsound(id)sender
{
[audioSound stop];
audioSound.currentTime=0;
[audioSound play]; //restart the player
}

How to stop the sound - Cocoa Touch

I have a sound looped, its a rect button, once the user presses the button the sound loops and plays. I want it so once the user has pressed the same button again, the sound stops playing.
This is the code
- (IBAction)threeSound:(id)sender; {
NSString *path = [[NSBundle mainBundle] pathForResource:#"3" ofType:#"wav"];
if (threeAudio) [threeAudio release];
NSError *error = nil;
threeAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
if (error)
NSLog(#"%#",[error localizedDescription]);
threeAudio.numberOfLoops = -1;
threeAudio.delegate = self;
[threeAudio play];
}
Thanks
Pretty straight forward...
- (IBAction)threeSound:(id)sender; {
if (threeAudio && threeAudio.playing) {
[threeAudio stop];
[threeAudio release];
threeAudio = nil;
return;
}
NSString *path = [[NSBundle mainBundle] pathForResource:#"3" ofType:#"wav"];
if (threeAudio) [threeAudio release];
NSError *error = nil;
threeAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
if (error)
NSLog(#"%#",[error localizedDescription]);
threeAudio.numberOfLoops = -1;
threeAudio.delegate = self;
[threeAudio play];
}
You should separate those two processes
into this
`- (void)initSound {
NSString *path = [[NSBundle mainBundle] pathForResource:#"3" ofType:#"wav"];
NSError *error = nil;
threeAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
if (error)
NSLog(#"%#",[error localizedDescription]);
threeAudio.numberOfLoops = -1;
threeAudio.delegate = self;
[threeAudio prepareToPlay];
[threeAudio play];
}
-(IBAction)threeSound:(id)sender:(UIButton *)sender{
if (sender.selected) {
[threeAudio pause];
} else {
threeAudio.currentTime = 0;
[threeAudio play];
}
sender.selected = !sender.selected;
}`
Note that initSound needs to be called at some point, if you want to start the sound with the button only.. then add
if (!threeAudio) {
[self initSound];
}
at the beggining of the IBAction
Thats my advice ;)

restarting a (audio) loop after it has finished

I want to play an audio loop by turning on a switch but i do not want the loop to end until the switch is turned off. When the loop ends i want it to simply start back at the beginning as to be continuos.
Try something like this. Note the -1 tells the music to repeat indefinitely:
Import this file at the top of your file:
#import <AVFoundation/AVFoundation.h>
Then to use it:
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:resourcePath ofType:#"caf"];
NSError *activationError = nil;
NSError *audioPlayerInitError = nil;
[[AVAudioSession sharedInstance] setActive: YES error:&activationError];
NSURL *newURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:newURL error:&audioPlayerInitError];
[newPlayer prepareToPlay];
[newPlayer setVolume:.8];
[newPlayer setNumberOfLoops:-1]; // -1 means play indefintely
[newPlayer setDelegate: self];
[newPlayer play];
[newPlayer release];
And then to stop it you would do:
if ([newPlayer isPlaying]) {
[newPlayer stop];
}