March 2013
Beginner to intermediate
580 pages
16h 11m
English
Create a new class in the com.bignerdranch.android.hellomoon package named AudioPlayer. Leave its superclass as java.lang.Object.
In AudioPlayer.java, add a member variable to hold an instance of MediaPlayer and methods to stop and play this instance.
Listing 13.6 Simple playback code using MediaPlayer (AudioPlayer.java)
public class AudioPlayer {
private MediaPlayer mPlayer;
public void stop() {
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
public void play(Context c) {
mPlayer = MediaPlayer.create(c, R.raw.one_small_step);
mPlayer.start();
}
}In the play(Context) method, you call MediaPlayer.create(Context, int). MediaPlayer needs the Context to make sense of the audio file’s resource ID. (There are other
Read now
Unlock full access