Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVAudioPlayer Won't Play Repeatedly

I have a relatively simple app that plays a sound using AVAudioPlayer, like this:

NSURL *file3 = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/ball_bounce.aiff", [[NSBundle mainBundle] resourcePath]]];
player3 = [[AVAudioPlayer alloc] initWithContentsOfURL:file3 error:nil];
[player3 prepareToPlay];

to later be called to play, like this:

[player3 play];

But this sound is called in multiple places (collisions with a bouncing ball), many times. Sometimes, the ball hits two things in quick enough succession where the sound from the first bounce is still playing when the ball collides a second time. This second bounce then does not produce a sound. How can I make it play a sound every time it collides, even if it involves paying a sound that overlaps an already playing sound?

Any help is greatly appreciated.

like image 260
user798370 Avatar asked Jun 18 '11 21:06

user798370


1 Answers

One way to solve this problem would to be create a new instance of AVAudioPlayer if the previous one is already playing:

if([player3 isPlaying]) {
   AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[player3 url]];
   [newPlayer setDelegate:self];
   [newPlayer play];
}

Then, in the delegate method audioPlayerDidFinishPlaying:successfully:, you should send the player the -release message:

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
   if(player != player3) {
      [player release];
   }
}

This is just the way it has to be, as you do need a new source buffer for each concurrent sound playing. :(

like image 124
Jacob Relkin Avatar answered Sep 28 '22 00:09

Jacob Relkin