Game Development Reference
In-Depth Information
By using a thin Java wrapper to the Mediatrack API, you can work around Android's lack of
support of popular audio standards such as OpenAL and others. With a wrapper you can
read the audio buffer from the native library and render it using the AudioTrack API. Thus, the
preceding steps get slightly modified as follows.
1.
The native game engine loads audio assets. This step remains
unchanged.
2.
The game engine initializes the audio hardware. This step can be
used not to initialize the audio hardware, but rather to tell the Android
Java code that audio has been initialized with a specific frequency,
resolution, and number of channels.
3.
The thin Java wrapper then uses a thread to read bytes from the
native audio buffer and render them to the hardware using an
AudioTrack.
This may not be the best way of doing things but it works quite well, as you'll see in the
chapters dealing with the 3D Quake engines later on in this topic. To illustrate the preceding
steps, let's consider a Java class dubbed NativeAudio (see Listing 2-4), which performs the
following tasks:
PaintAudio(ByteBuffer buf) , that
reads the audio buffer from the native library.
It defines a native audio painter,
It implements the following methods for audio playback:
start(freq, channels, bits, bufferSize) : Here is where the good stuff
happens. This method starts a thread that reads the audio buffer from the
native engine (by calling PaintAudio). The thread will loop continuously until
it is told to stop using a boolean flag. The arguments to this sub are: sound
frequency in Hertz, the number of channels (1 for mono, 2 for stereo), sound
resolution in bits, and the size of the buffer.
stop() : This subroutine is used to stop the audio thread from Java.
OnInitAudio(freq, channels, bits) : This is one of a series of magic subs
called from C that tell the Java code it is time to start playing. This function
will be called whenever the native library initializes the audio. It receives the
frequency, number of channels, and resolution. It then calls the start method
with those arguments.
OnLockAudio()/OnUnlockAudio() : These methods are used to lock/unlock the
audio when the user pauses the game for example. They simply tell the main
thread when to read data.
OnCloseAudio() : This function is fired from the native side to close the audio
and cleanup resources.
 
Search WWH ::




Custom Search