Game Development Reference
In-Depth Information
public void
setLooping(
boolean
isLooping) {
mediaPlayer.setLooping(isLooping);
}
public void
setVolume(
float
volume) {
mediaPlayer.setVolume(volume, volume);
}
The
setLooping()
and
setVolume()
methods can be called in any state of the
MediaPlayer
and
delegated to the respective
MediaPlayer
methods.
public void
stop() {
mediaPlayer.stop();
synchronized
(
this
) {
isPrepared=
false
;
}
}
The
stop()
method stops the
MediaPlayer
and sets the
isPrepared
flag in a synchronized block.
public void
onCompletion(MediaPlayer player) {
synchronized
(
this
) {
isPrepared=
false
;
}
}
}
Finally, there's the
OnCompletionListener.onCompletion()
method that is implemented by the
AndroidMusic
class. All it does is set the
isPrepared
flag in a synchronized block so that the
other methods don't start throwing exceptions out of the blue. Next, we'll move on to our input-
related classes.
AndroidInput and AccelerometerHandler
Using a couple of convenient methods, the
Input
interface we designed in Chapter 3 grants us
access to the accelerometer, the touchscreen, and the keyboard in polling and event modes.
The idea of putting all the code for an implementation of that interface into a single file is a bit
nasty, so we outsource all the input event handling to handler classes. The
Input
implementation
will use those handlers to pretend that it is actually performing all the work.
AccelerometerHandler: Which Side Is Up?
Let's start with the easiest of all handlers, the
AccelerometerHandler
. Listing 5-5 shows its code.
Listing 5-5. AccelerometerHandler.java; Performing All the Accelerometer Handling
package
com.badlogic.androidgames.framework.impl;
import
android.content.Context;
import
android.hardware.Sensor;