Game Development Reference
In-Depth Information
Listing 11-6. Sample 11-Prefix.pch (partial)
#define AUDIO_POWERUP_BLINK @"powerup_blink"
#define AUDIO_GOT_POWERUP @"got_powerup"
#define AUDIO_BC_THEME @"bc_theme"
In Listing 11-6, we see that we have defined three constants. Each is simply a string referring to the
name of an audio file included in the project, excluding the file extension. Figure
11-3
shows these
files in the Xcode project.
Figure 11-3.
Including Audio in an Xcode Project
In Figure
11-3
, we see that, to include audio in a project, we simply add the files as we would any
other resource. Also, note that they are all m4a files. iOS is capable of playing many types of audio
files, but AAC-encoded files are recommended, which these are. In our example, the m4a extension
is assumed.
1
Looking back at Listing 11-5, we see that we pass these constants to two different tasks,
prepareAudio
: and
playBackgroundAudio
:. The task
prepareAudio
: gets an audio file ready
so that the audio is played promptly when a game event requires a sound effect. The task
playBackgroundAudio
: loops a single track forever to provide background music for a game. Let's
start with
prepareAudio
:, as shown on Listing 11-7.
1
There is a lot more to know about audio formats and encodings. Here is a good starting place to learn