O'Reilly logo

Programming Android by Zigurd Mednieks, G. Blake Meike, Masumi Nakamura, Laird Dornin

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

Playing Audio and Video

Android provides a standard means to play audio or video: the MediaPlayer class. For audio content, you can also play back raw data, which is useful in sophisticated applications where you generate the audio dynamically.

A MediaPlayer goes through several states during its life cycle:

Idle

The MediaPlayer is instantiated.

Initialized

The media source is set.

Preparing

The MediaPlayer is preparing the media source for playback.

Prepared

The MediaPlayer is prepared for playback.

Started

Playback is in progress.

Paused

Playback has been paused.

Playback complete

Playback of source is done (the playback can be started again).

Stopped

The MediaPlayer is no longer prepared to play the source.

End

The MediaPlayer is no more, and all associated resources are released.

For details on these states, view the state diagram provided on the Developers site at http://developer.android.com/reference/android/media/MediaPlayer.html#StateDiagram. To get started with MediaPlayer, it’s useful at this point to view it as a series of steps in your application:

  1. Create a MediaPlayer instance through the create() method (idle state).

  2. Initialize the MediaPlayer with the media source to play (initialized state).

  3. Prepare the MediaPlayer for playback through the prepare() method (preparing and prepared states).

  4. Play the MediaPlayer through the start() method (started state).

  5. During playback, if desired, you can pause, stop, or replay the MediaPlayer (started, paused, playback complete, and stopped states).

  6. Once ...

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