Game Development Reference
In-Depth Information
Listing 11-1 . Preloading a background track and playing it at a later time
OALSimpleAudio* audio = [OALSimpleAudio sharedInstance];
[audio preloadBg:@"Audio/menu-music.m4a" seekTime:0.0];
// later you can play back the most recently preloaded
track:
[audio playBgWithLoop:YES];
In the OALSimpleAudio interface, there is no delegate mechanism or notification that
tells you when preloading has finished, nor is there a need to have one. Preloading is not
multithreaded. In the preceding example, execution will not continue to the
playBgWithLoop method unless preloadBg has done its job and already preloaded
the file.
Caution As with other resource files, the audio file is in a subfolder in the
SpriteBuilder project. Thus, the Audio folder needs to be specified in the path.
And if the audio file's output type is set to MP4, its file extension will be .m4a
(audio-only MPEG-4) and not .mp4 (regular audio/video MPEG-4).
In addition to that, specifying the published file extensions (.m4a and .caf) as in
these examples is not recommended if you plan to port to Android. All Audio
files for Android will be published in the OGG format with the .ogg extension.
By the time you read this, Cocos2D respectively ObjectAL will allow you to
omit the audio file extension altogether or you can use the audio file's original
extension (.wav).
In most cases, it's simpler and fast enough to just play the background music directly.
Open the MainScene.m file in Xcode, and locate the didLoadFromCCB method. Add
the lines highlighted in Listing 11-2 to the end of the method.
Listing 11-2 . Adding music playback to MainScene.m
-(void) didLoadFromCCB
{
// existing code omitted for brevity ...
OALSimpleAudio* audio = [OALSimpleAudio sharedInstance];
[audio playBg:@"Audio/menu-music.m4a" loop:YES];
}
Search WWH ::




Custom Search