Learning about OALSimpleAudio

If you've ever wanted an easy way to play sound files, OALSimpleAudio is the thing you need. It can very easily load a large variety of sound files, play effects, loop background music, and a lot more. Its existence and integration with Cocos2d makes it much easier to bring your game to life with the immersive capabilities of sound and music.

Tip

If you've programmed in Cocos2d before and are wondering where SimpleAudioEngine is, starting in Cocos2d v3.0, OALSimpleAudio is the new way to play sound effects. It's basically everything that SimpleAudioEngine was.

Preloading effects

If you try to play an effect using OALSimpleAudio, there will be a slight freeze, or delay, as the device tries to quickly load the effect into the memory and then play it right away. Luckily, there's a way to load the sound effects and music such that it doesn't freeze in front of your users when you try to play an effect.

OALSimpleAudio allows preloading of effects, which essentially reads the sound effect into the memory long before you'll need the effect. The choice is up to you whether to do this at the beginning of the game (when users first launch it from their home screen), or between levels by unloading and reloading the effects for the upcoming level. The way to load sound files into the memory with OALSimpleAudio is by adding the following line of code:

ALBuffer *buffer = [[OALSimpleAudio sharedInstance] preloadEffect:@"soundEffect.mp3"];

The buffer variable assignment is optional and is used if you need to print out various pieces of information about the sound file such as frequency or bits of the buffer.

Tip

Although the preceding code example shows .mp3 as the file extension, OALSimpleAudio can load any sound file that's supported by iOS.

However, if you want to cut down on the time it takes to load all your in-game sounds, you can do so in the background, which is known as loading asynchronously.

Loading files asynchronously

Loading your files asynchronously is the best way to cut down on load time while simultaneously getting all the files loaded. However, note that because loading this way occurs in the background, there's no guarantee that the files will be ready when the user begins to interact with your game.

If you wish to make certain sound effects available for them at the beginning of the game (at the loading screen, before the main menu starts, right when the main menu starts, or whatever you see the beginning of your game as), it's recommended to load the minimum amount of sound needed if you still wish to load the majority of your effects asynchronously.

The way to do this is with the following line of code. It will push the loading into the background and notify you when it's done:

__block ALBuffer *soundBuffer;
[[OALSimpleAudio sharedInstance] preloadEffect:@"soundEffect.mp3" reduceToMono:NO completionBlock:^(ALBuffer* buffer)
{
  soundBuffer = buffer;
}];

Unloading effects

If you know you're not going to use the sound effect for a while, or you often run into memory warnings, unloading your sound effects can be useful. For example, if your game uses a certain sound file for a voice-over in the tutorial only, once the user passes the tutorial, you can unload this sound effect to free up some memory. OALSimpleAudio will not unload sound effects that are either playing or paused.

To unload a specific sound effect, you can use this line of code:

[[OALSimpleAudio sharedInstance] unloadEffect:@"soundEffect.mp3"];

To unload all your sound effects at once, you can use the following line of code:

[[OALSimpleAudio sharedInstance] unloadAllEffects];

Tip

A recommended place to put these unloading calls is your applicationDidReceiveMemoryWarning method in the AppDelegate class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.35.247