Game Development Reference
In-Depth Information
6.3 Adding Background Music
Android developers can also utilize MediaPlayer to control playback of audio/video files
and streams. The table 6.2 below shows the public methods of MediaPlayer that are com-
monly used in programming.
Method
Description
create()
Creates a MediaPlayer for a given resource id.
start()
Starts or resumes the playback. Successful invoke of this method in a val-
id state transfers the object to the
Started
state. Calling this method in an
invalid state transfers the object to the
Error
state.
pause()
Pauses the playback. Successful invoke of this method in a valid state
transfers the object to the
Paused
state. Calling this method in an invalid
state transfers the object to the
Error
state.
stop()
Stops playback after playback has been stopped or paused. Successful in-
voke of this method in a valid state transfers the object to the
Stopped
state. Calling this method in an invalid state transfers the object to the
Er-
ror
state.
release()
Releases resources associated with this MediaPlayer object. After
re-
lease(),
the object is no longer available.
isLooping()
Checks whether the MediaPlayer is looping or non-looping.
setLooping()
Sets the player to be looping or non-looping. Successful invoke of this
method in a valid state does not change the state. Calling this method in
an invalid state transfers the object to the
Error
state.
isPlaying()
Checks whether the MediaPlayer is playing. Successful invoke of this
method in a valid state does not change the state. Calling this method in
an invalid state transfers the object to the
Error
state.
Table 6.2 MediaPlayer's Public Methods
Let's see the snippet. First, add the background music by calling
create()
method and set the
player to be looping in the main activity:
Search WWH ::
Custom Search