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​ ...

Get Android Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.