O'Reilly logo

Android Programming: The Big Nerd Ranch Guide by Brian Hardy, Bill Phillips

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

Audio Playback

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)

p​u​b​l​i​c​ ​c​l​a​s​s​ ​A​u​d​i​o​P​l​a​y​e​r​ ​{​ ​ ​ ​ ​p​r​i​v​a​t​e​ ​M​e​d​i​a​P​l​a​y​e​r​ ​m​P​l​a​y​e​r​;​ ​ ​ ​ ​p​u​b​l​i​c​ ​v​o​i​d​ ​s​t​o​p​(​)​ ​{​ ​ ​ ​ ​ ​ ​ ​ ​i​f​ ​(​m​P​l​a​y​e​r​ ​!​=​ ​n​u​l​l​)​ ​{​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​m​P​l​a​y​e​r​.​r​e​l​e​a​s​e​(​)​;​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​m​P​l​a​y​e​r​ ​=​ ​n​u​l​l​;​ ​ ​ ​ ​ ​ ​ ​ ​}​ ​ ​ ​ ​}​ ​ ​ ​ ​p​u​b​l​i​c​ ​v​o​i​d​ ​p​l​a​y​(​C​o​n​t​e​x​t​ ...

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