AVAudioPlayer causes my game to lag

删除回忆录丶 提交于 2019-12-12 02:08:00

问题


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; 
}

回答1:


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



来源:https://stackoverflow.com/questions/27218257/avaudioplayer-causes-my-game-to-lag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!