Game Development Reference
In-Depth Information
AudioClip wraps and instance of MediaPlayer and is aware of the state of the sound: paying, paused,
or looping. This awareness is necessary for dealing with IllegalState exceptions explained previously.
AudioClip also listens for completion events via setOnCompletionListener(OnCompletionListener) , and if
the loop flag is set, it will replay the sound. Space Blaster has three sounds that can be loaded with the
code:
blast = getAudioClip(R.raw.sb_blast); // Laser shot
crash = getAudioClip(R.raw.sb_collisn); // Ship collision
kill = getAudioClip(R.raw.sb_mdestr); // meteor destruction
getAudioClip takes a sound resource ID as argument, and it is defined in the abstract class
ArcadeGame as follows:
protected AudioClip getAudioClip(int id) {
return new AudioClip(mContex, id);
}
Tip Audio resources must be saved in the res/raw folder within the project.
About Native Sound
Native games like Doom and Wolf 3D, shown in later chapters, pack sound resources in their own
formats and manipulate the sound device at the operating system level. Even though Android runs a
Linux kernel behind the scenes, it is not a standard Linux distribution, especially with dealing with low-
level devices, for example:
The standard Linux sound device ( /dev/dsp ) and mixer ( /dev/mixer ) are not
supported, making reusing sound logic very difficult for native games. For some
reason, Google decided to use the Sonivox Enhanced Audio System (EAS) via the
/dev/eac device. This caveat is compounded by the fact that Google provides no
support for native development. Nevertheless, a solution is to cascade the sound
name back to Java via JNI and play it using AudioClip . This technique is explained
in detail in Chapters 6 and 7.
If you have native code that uses OpenAL/OpenGL for accelerated sound and
graphics, you are in trouble. Google wants all development done in its Java-based
version of OpenGL and doesn't support OpenAL. This leaves you with the painful
task of translating OpenGL logic line by line from C to Java. Nevertheless, all is not
lost. I found a neat trick to leave your C-based OpenGL code intact and still be
able to run it from Java, and I explain my trick in 21.330Chapter 5.
Search WWH ::




Custom Search