Hey everybody, I have a drum app that I've created basing it off of AVAudioPlayer instead of the common system sounds in order to have a bit of control.
The problem is that whenever 2/more sounds are played at once, it lags and stops alls sounds, so your drum beat gets choppy and randomly stopped.
like boom boom chhhhh boom boom ch--- boom bo---- chhhhh
If you can tell at all from that onomatopoeia.
Here's the sound code, I have an array of preloaded AVAudioPlayers so that it loads faster.
- (void)triggerSound:(NSInteger)soundNumber {
NSInteger deltaNum = soundNumber*numberOfBuffers;
AVAudioPlayer *lowBuffer = [bufferBox objectAtIndex:deltaNum];
Boolean soundFired = FALSE;
// Find an unused buffer if possible, otherwise play sound from first buffer.
for (int i=0; i<numberOfBuffers; i++) {
NSLog(@"Buffer loop: %d", i);
NSLog(@"Buffer to load: %d", deltaNum);
AVAudioPlayer *tempBuffer = [bufferBox objectAtIndex:deltaNum+i];
if (!tempBuffer.playing) {
tempBuffer.currentTime = 0;
[tempBuffer play];
soundFired = TRUE;
break;
} else if (lowBuffer.currentTime>tempBuffer.currentTime) {
lowBuffer = tempBuffer;
}
}
if (!soundFired) {
lowBuffer.currentTime = 0;
[lowBuffer play];
soundFired = TRUE;
}
}
That method is called in the IBAction button press. ex: [drumObject triggerSound:3].
All help appreciated!