Introducing the Media Player

Multimedia playback in Android is handled by the MediaPlayer class. You can play media stored in application resources, local files, Content Providers, or streamed from a network URL. In each case, the file format and type of multimedia being played is abstracted from you as a developer.

The Media Player's management of audio and video files and streams is handled as a state machine. In the most simplistic terms, transitions through the state machine can be described as follows:

> Initialize the Media Player with media to play.

> Prepare the Media Player for playback.

> Start the playback.

> Pause or stop the playback prior to its completing.

> Playback complete.

A more detailed and thorough description of the Media Player state machine is provided at the Android developer site at http://developer.android.com/reference/android/media/MediaPlayer .html#StateDiagram

To play a media resource you need to create a new MediaPlayer instance, initialize it with a media source, and prepare it for playback.

The following section describes how to initialize and prepare the Media Player. After that, you'll learn to control the playback to start, pause, stop, or seek the prepared media.

In each case, once you've finished playback, caltelease on your Media Player object to free the associated resources:

mediaPlayer.release();

Android supports a limited number of simultaneous Media Player objects; not releasing them can cause runtime exceptions when the system runs out of resources.

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Post a comment