O'Reilly logo

Hello, Android, 4th Edition by Ed Burnette

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Music to Play By

Playing a different song during the game would be a nice change of pace, so let’s make a similar change to GameActivity.java. While we’re there, we’ll add a sound that plays when the game is over. First we need to declare the variable for the media player and a handler we’ll need later:

ticTacToev4/src/main/java/org/example/tictactoe/GameActivity.java
 
import​ android.media.MediaPlayer;
 
import​ android.os.Handler;
 
 
public​ ​class​ GameActivity ​extends​ Activity {
 
private​ MediaPlayer mMediaPlayer;
 
private​ ​Handler​ mHandler = ​new​ ​Handler​();
 
// ...
 
}

Then we start and stop the music in onResume() and onPause() as before:

ticTacToev4/src/main/java/org/example/tictactoe/GameActivity.java
 
@Override
 
protected ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required