I read about preloading sounds in SimpleAudioEngine through
[[SimpleAudioEngine sharedEngine] preloadEffect:@"bell.wav"];
But, with this there are memory leaks which I read on couple of sites. On some websites it was used like this: –
-(void)loadSoundFilesInBackground
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[[SimpleAudioEngine sharedEngine] preloadEffect:@"bell.wav"];
[pool release];
}
What is the best way to load sound files in background through SimpleAudioEngine ? What is the use of NSAutoreleasePool here? Is it really required to load sound files in background? I could not find satisfactory answer on any website.
Since a lot of information about Cocos2D is quite old I wouldn’t believe a website that says that this method leaks memory. First, this might have been fixed already. Secondly, it may not be a “true” leak after all. Some developers just don’t know how to read the Instruments graphs, or test the issue in isolation.
To answer your first question: you preload sound effects so that the file doesn’t have to be loaded the first time you play that sound. For example, if you didn’t preload the 10 different “shoot” sounds for each enemy type in your game, every time a new enemy type shoots for the first time you may notice a small delay. This can be negligible or even halting your game for several tenths of a second, and can lead to controls not responding or sudden death situations because the player isn’t prepared for such unexpected hiccups.
Thus fast paced games should preload all gameplay sound effects before the game commences.