спасибо
спасибо

Reputation: 89

AVAudioPlayer causes my game to lag

I've made a simple game in Xcode where the character moves around the screen to collect coins, while avoiding getting hit by rocks.

For the movement I use NSTimers which work fine. The problem occurs when I add sound effects. I use AVAudioPlayers for this and it makes the game lag a little bit. I commented them out to see if they caused the problem, and it seems to be the case. I've looked around here on Stack Overflow and on other sites, but I can't seem to 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; 
}

Upvotes: 0

Views: 178

Answers (1)

спасибо
спасибо

Reputation: 89

I figured it out. I used System Sound Services instead of AVAudioPlayer, and now it works perfectly.

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

Upvotes: 1

Related Questions