Game Development Reference
In-Depth Information
UInt32 otherAudioIsPlayingVal;
UInt32 propertySize = sizeof (otherAudioIsPlayingVal);
AudioSessionGetProperty (kAudioSessionProperty_OtherAudioIsPlaying,
&propertySize,
&otherAudioIsPlayingVal
);
otherAudioIsPlaying = (BOOL)otherAudioIsPlayingVal;
if (otherAudioIsPlaying){
[backgroundPlayer pause];
} else {
[backgroundPlayer play];
}
AVAudioSession
by calling
. We assign a category to this application by calling
setCategory
and specifying
. This indicates that the game is willing to allow audio from other
AudioSessionGetProperty
and populating the
UInt32 otherAudioIsPlayingVal
UInt32
to a more Objective-C friendly
BOOL
and store the value in
.
Lastly, we either play or pause our background music depending on whether the other audio is
playing. We do this by calling the appropriate task on
backgroundPlayer
, which is an
AVAudioPlayer
defined elsewhere. (We will get back to
backgroundPlayer
later in this chapter.)
initializeAudio
is called in the
doSetup
task of
GameController
, so we know it will be called before
our game gets under way. However, it is possible for users to start or stop their audio after the game
has been initialized. The following section shows how we make sure our application is always in the
correct state regarding the user's choice of audio.
Responding to Other Audio Changes
Say the user starts the game without his own audio, plays for a while, and then decides to listen to
his own audio. He might do this by hitting the Home button twice, swiping over to the audio controls,
hitting Play, and then hitting Home twice again to return to the game. Therefore, we want to make
sure we adjust the audio behavior of our application appropriately. Figure
11-1
shows an iPhone in
the processing of doing just this.