how to play only one sound at a time with AVAudioplayer
i am playing sound when user tap on the respected button.
i have 10 buttons with 10 audio files.
i am playing the selected sound by using the AVAudioPlayer
AVAudioPlayer *myaudio=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:[sounds objectAtIndex:button.tag] ofType:@"mp3"]]error:NULL];
[myaudio play];
Now i have a problem that if i click another button before completion of playi开发者_Go百科ng previous sound then both sounds are mixed and getting noise.
I need to play second sound after completion of first sound.
i mean at a time only one sound will be played.
How can i done,can any one please help me.
Let me add comment if this question is not understandable.
Thank u in advance.
i resolve my problem like this
if (soundplay == 0) {
myaudio=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:[sounds objectAtIndex:x] ofType:@"mp3"]]error:NULL];
myaudio.delegate = self;
soundplay = 1;
[myaudio play];
}
- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *)myaudio successfully:(BOOL)flag {
soundplay = 0;
NSLog(@"sound fnished");
}
where soundplay is integer variable
@Mahesh you can create a AVAudioPlayer
class variable and in the IBAction function you can do something like this:
if([myaudio playing])
[myaudio stop];
myaudio=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:[sounds objectAtIndex:button.tag] ofType:@"mp3"]]error:NULL];
[myaudio play];
dont create different objects of AVAudioPlayer . create singletone object for AVAudioPlayer . and using this object just check with that object player is playing or not .
if([myaudio playing])
{
[myaudio stop];
myaudio=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:[sounds objectAtIndex:button.tag] ofType:@"mp3"]]error:NULL];
[myaudio play];
}
精彩评论