I have 6 buttons that each play an audio sample from a .caf file. If I press a button the sound plays fine, if I wait for it to end and press it again it plays fine but if I press the button fast then the sound will pop and click before playing.
I initially didn't have this popping problem when simply allocating the AVAudioPlayer each time the button is clicked but this created a memory leak with multiple allocations. So I created 6 AVAudioPlayers for each button and re-used it, this got rid of the memory leak but now the samples click/pop when overwritten.
I have tried lots of different ways to stop this happening from setting the volume to 0, stopping the AVAudioPlayer instance before playing the next sample etc but cant find the correct way to repeatedly play the same sample sound with fast button presses and stop the popping.
I do have the AVAudioPlayer property as retain in the .h and use autorelease in the alloc statement.
Any help please?
** Edit: found a solution, its not pretty but it works.
Basically I created 10 AVAudioPlayers that autorelease and if one [myPlayer1 isPlaying] then I use the next one.
e.g.
BOOL done = NO;
if(![self.audioPlayer0 isPlaying] && done == NO) {
self.audioPlayer0 = [ [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil] autorelease ];
[audioPlayer0 play];
done = YES;
}
if(![self.audioPlayer1 isPlaying] && done == NO) {
self.audioPlayer1 = [ [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil] autorelease ];
[audioPlayer1 play];
done = YES;
}
if(![self.audioPlayer2 isPlaying] && done == NO) {
self.audioPlayer2 = [ [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil] autorelease ];
[audioPlayer2 play];
done = YES;
}
if(![self.audioPlayer3 isPlaying] && done == NO) {
self.audioPlayer3 = [ [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil] autorelease ];
[audioPlayer3 play];
done = YES;
}
if(![self.audioPlayer4 isPlaying] && done == NO) {
self.audioPlayer4 = [ [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil] autorelease ];
[audioPlayer4 play];
done = YES;
}
if(![self.audioPlayer5 isPlaying] && done == NO) {
self.audioPlayer5 = [ [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil] autorelease ];
[audioPlayer5 play];
done = YES;
}
if(![self.audioPlayer6 isPlaying] && done == NO) {
self.audioPlayer6 = [ [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil] autorelease ];
[audioPlayer6 play];
done = YES;
}
if(![self.audioPlayer7 isPlaying] && done == NO) {
self.audioPlayer7 = [ [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil] autorelease ];
[audioPlayer7 play];
done = YES;
}
if(![self.audioPlayer8 isPlaying] && done == NO) {
self.audioPlayer8 = [ [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil] autorelease ];
[audioPlayer8 play];
done = YES;
}
if(![self.audioPlayer9 isPlaying] && done == NO) {
self.audioPlayer9 = [ [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil] autorelease ];
[audioPlayer9 play];
}