iPhone – Playing multiple sounds at a time?

I have 6 sounds on one view.

But I want it so that I can play multiple at the same time, so you hit sound 1 (Sound 1 is playing) and then sound 2 is playing. And sound 1 is still playing.

But at the moment I press sound 1 (sound 1 is played), press sound 2 (sound 2 is playing, but sound 1 is stopped )

This is the code of the audio part.

- (IBAction)oneSound:(id)sender; {
NSString *path = [ [NSBundle mainBundle] pathForResource:@"1" ofType:@"wav"];
if (theAudio) [theAudio release];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
volumeTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(updateVolume) userInfo:nil repeats: YES];
}

- (IBAction)twoSound:(id)sender; {
NSString *path = [[NSBundle mainBundle] pathForResource:@"2" ofType:@ "wav"];
if (theAudio) [theAudio release];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[t heAudio play];
volumeTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(updateVolume) userInfo:nil repeats:YES];
}

There are some important codes missing, but it seems that the audio is a global one, and you use it to manage your sound playback. Because you will It is destroyed, so any content currently playing will stop in preparation for the next sound.

There are several ways to solve this problem, one of which is that each sound has its own uniqueness AVAudioPlayer instance.

I have 6 sounds on one view.

But I want it so that I can play multiple at the same time , So you tap on sound 1 (sound 1 is playing) and then sound 2 plays. And sound 1 is still playing.

But at the moment I press sound 1 (sound 1 is played), and sound 2 (sound 2 plays, but the sound 1 stops)

This is the code of the audio part.

- (IBAction)oneSound:(id)sender; (
NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"wav"];
if (theAudio) [theAudio release];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL :[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
volumeTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector( updateVolume) userInf o:nil repeats:YES];
}

- (IBAction)twoSound:(id)sender; {
NSString *path = [[NSBundle mainBundle] pathForResource:@" 2" ofType:@"wav"];
if (theAudio) [theAudio release];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
volumeTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(updateVolume) userInfo:nil repeats:YES];
}

There are some important codes missing, but it seems that the audio is a global one, and you use it to manage your sound playback. Because you will It is destroyed, so any content currently playing will stop in preparation for the next sound.

There are several ways to solve this problem, one of which is that each sound has its own uniqueness AVAudioPlayer instance.

Leave a Comment

Your email address will not be published.