Game Development Reference
In-Depth Information
This directly translates into the
Audio
,
Music
, and
Sound
interfaces (shown in Listings 3-3
through 3-5, respectively).
Listing 3-3. The Audio Interface
package
com.badlogic.androidgames.framework;
public interface
Audio {
public
Music newMusic(String filename);
public
Sound newSound(String filename);
}
The
Audio
interface is our way to create new
Music
and
Sound
instances. A
Music
instance
represents a streamed audio file. A
Sound
instance represents a short sound effect that we keep
entirely in memory. The methods
Audio.newMusic()
and
Audio.newSound()
both take a filename
as an argument and throw an
IOException
in case the loading process fails (for example,
when the specified file does not exist or is corrupt). The filenames refer to asset files in our
application's APK file.
Listing 3-4. The Music Interface
package
com.badlogic.androidgames.framework;
public interface
Music {
public void
play();
public void
stop();
public void
pause();
public void
setLooping(
boolean
looping);
public void
setVolume(
float
volume);
public boolean
isPlaying();
public boolean
isStopped();
public boolean
isLooping();
public void
dispose();
}
The
Music
interface is a little bit more involved. It features methods to start playing the
music stream, pausing and stopping it, and setting it to loop playback, which means it will
automatically start from the beginning when it reaches the end of the audio file. Additionally,
we can set the volume as a float in the range of 0 (silent) to 1 (maximum volume). A couple of
getter methods are also available that allow us to poll the current state of the
Music
instance.
Once we no longer need the
Music
instance, we have to dispose of it. This will close any system
resources, such as the file from which the audio was streamed.