AVAudioPlayer makes my game lag

I made a simple game in Xcode where the character moves around the screen to collect coins, avoiding stones.

For traffic, I use NSTimers, which work fine. The problem occurs when I add sound effects. I use AVAudioPlayers for this and it is a little behind in the game. I commented on them to find out if they caused a problem, and it seems to be so. I looked around here on Qaru and other sites, but I can not find a good answer. Any help appreciated!

code:

- (void)CharacterMoving { if (CGRectIntersectsRect(Character.frame, Rock.frame)) { [self performSelector:@selector(GameOver) withObject:nil afterDelay:0.35]; [Thousand play]; } - (IBAction)Start:(id)sender { NSString *soundEffect=[[NSBundle mainBundle]pathForResource:@"1000" ofType:@"mp3"]; Thousand=[[AVAudioPlayer alloc]initWithContentsOfURL: [NSURL fileURLWithPath:soundEffect] error:NULL]; Thousand.numberOfLoops=-0; } 
0
source share
1 answer

I get it. I used System Sound Services instead of AVAudioPlayer and now it works great.

This was very useful: https://developer.apple.com/library/mac/documentation/AudioToolbox/Reference/SystemSoundServicesReference/index.html

+1
source

Source: https://habr.com/ru/post/1264743/


All Articles