Game Development Reference
In-Depth Information
public SharedPreferences getPreferences() {
return PreferenceManager.getDefaultSharedPreferences(context);
}
}
Everything is straightforward. We implement the
FileIO
interface, store the
Context
instance,
which is the gateway to almost everything in Android, store an
AssetManager
, which we pull
from the
Context
, store the external storage's path, and implement the four methods based on
this path. Finally, we pass through any
IOException
is that get thrown so we'll know if anything is
irregular on the calling side.
Our
Game
interface implementation will hold an instance of this class and return it via
Game.getFileIO()
. This also means that our
Game
implementation will need to pass through the
Context
in order for the
AndroidFileIO
instance to work.
Note that we do not check if the external storage is available. If it's not available, or if we forget
to add the proper permission to the manifest file, we'll get an exception, so checking for errors is
implicit. Now, we can move on to the next piece of our framework, which is audio.
AndroidAudio, AndroidSound, and AndroidMusic:
Crash, Bang, Boom!
In Chapter 3, we designed three interfaces for all our audio needs:
Audio
,
Sound
, and
Music
.
Audio
is responsible for creating
sound
and
Music
instances from asset files.
Sound
lets us play
back sound effects that are stored in RAM, and
Music
streams bigger music files from the disk
to the audio card. In Chapter 4, you learned which Android APIs are needed to implement this.
We will start with the implementation of
AndroidAudio
, as shown in Listing 5-2, interspersed with
explanatory text where appropriate.
Listing 5-2. AndroidAudio.java; Implementing the Audio Interface
package
com.badlogic.androidgames.framework.impl;
import
java.io.IOException;
import
android.app.Activity;
import
android.content.res.AssetFileDescriptor;
import
android.content.res.AssetManager;
import
android.media.AudioManager;
import
android.media.SoundPool;
import
com.badlogic.androidgames.framework.Audio;
import
com.badlogic.androidgames.framework.Music;
import
com.badlogic.androidgames.framework.Sound;
public class
AndroidAudio
implements
Audio {
AssetManager assets;
SoundPool soundPool;