This issue appeared in several other issues here: SO /
Slow launch for AVAudioPlayer when playing audio for the first time
Delay in playing sounds using AVAudioPlayer
I tried to implement the fixes, but none of them solves my problem. My application presents to the user a grid of objects to touch when the object touches the sound that is being played. This works great, except when there is a delay of ~ 2 seconds with the initial touch.
To get around this, I initialize my audio player using the dummy aiff file:
- (void)viewDidLoad{ NSString *soundFile = [Card loadCardSoundPath:@"dummy"]; AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:soundFile] error:nil]; self.audioPlayer = player; [audioPlayer setDelegate:self]; [audioPlayer prepareToPlay];
Then, when the object is touching, I call:
NSString *soundFile = [Card loadCardSoundPath:name]; if (soundFile != nil){ AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:soundFile] error:nil]; self.audioPlayer = player; [self.audioPlayer setDelegate:self]; [self.audioPlayer prepareToPlay]; [self.audioPlayer play]; [player release]; }
The behavior that I am observing is the following: I am observing a delay in sound when I click on the first object if I create a dummy audio player and call prepareToPlay but not playing. However, if I call the game on a dummy audio player, I do not get a delay on the original object, but the view loading is delayed by 2 seconds.
Is there any way around this? Should I just create an NSArray from AVAudioPlayers when I load it and tell them everything in order to get ready for the game, and then just trigger playback when the object is clicked?
source share