I have code like the following and I see problems where occasionally (mabye 5% of the time) when I attempt to play a sound it either doesn't start at all or it stops early. This happens on the simulator as well as the physical device. I'm not using and concurrency or anything like that, and I only play a few sounds.
(void)playYesNoSound {
NSString *yesNoSoundPath = [[NSBundle mainBundle] pathForResource:@"YesNoBeat" ofType:@"wav"];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:yesNoSoundPath] error:nil];
[player prepareToPlay];
activePlayer = player;
player.delegate = self;
[player play];
}
-(void)playMistSound {
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"MysticFogNight" ofType:@"wav"];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:nil];
activePlayer = player;
[player prepareToPlay];
player.volume = 0.1;
player.numberOfLoops = -1; /
player.delegate = self;
[player play];
}
I have tried initializing the players prior to the calls to the play methods but it makes no difference. I have also tried using AVAudioEngine with AVAudioPlayerNode directly, but I still see the same problems. Has anyone else ecountered inconsistencies like this?