English | Site Directory

Android - An Open Handset Alliance Project

android.media
public class

android.media.MediaPlayer

java.lang.Object
android.media.MediaPlayer

The MediaPlayer class is used for playing audio and video files and streams.

Nested Classes
MediaPlayer.OnBufferingUpdateListener Interface definition of a callback to be invoked indicating buffering status of a media resource being streamed over the network. 
MediaPlayer.OnCompletionListener Interface definition for a callback to be invoked when playback of a media file has completed. 
MediaPlayer.OnErrorListener Interface definition of a callback to be invoked when there has been an error during an asynchronous operation (other errors will throw exceptions at method call time). 
MediaPlayer.OnPreparedListener Interface definition for a callback to be invoked when the media file is ready for playback. 

Summary

Public Constructors

          MediaPlayer()

Public Methods

      static  MediaPlayer  create(Context context, Uri uri)
Convenience method to create a MediaPlayer for a given Uri.
      static  MediaPlayer  create(Context context, int resid)
Convenience method to create a MediaPlayer for a given resource id
      static  MediaPlayer  create(Context context, Uri uri, Surface surface)
Convenience method to create a MediaPlayer for a given Uri.
        int  getCurrentPosition()
Gets the current playback position.
        int  getDuration()
Gets the duration of the file.
        Bitmap  getFrameAt(int msec)
Returns a Bitmap containing the video frame at the specified time.
        boolean  isPlaying()
Returns true when the MediaPlayer is currently playing, and false if it hasn't been started or is paused.
        void  pause()
Pauses playback.
        void  prepare()
Prepares the player for playback, synchronously.
        void  prepareAsync()
Prepares the player for playback, asynchronously.
        void  release()
Releases resources associated with this MediaPlayer object.
        void  reset()
Resets the MediaPlayer to its uninitialized state.
        void  seekTo(int msec)
Seeks to specified position.
        void  setAudioStreamType(int streamtype)
Sets the audio stream type for this MediaPlayer.
        void  setDataSource(String path)
Sets the data source (file-path or URL) to use.
        void  setDataSource(FileDescriptor fd, String mimeType)
Sets the data source (FileDescriptor) to use.
        void  setDisplay(Surface sv)
Sets the Surface to use for displaying the video portion of the media.
        void  setLooping(int looping)
Sets the player to be looping or non-looping.
        void  setOnBufferingUpdateListener(OnBufferingUpdateListener l)
Register a callback to be invoked when the status of a network stream's buffer has changed.
        void  setOnCompletionListener(OnCompletionListener l)
Register a callback to be invoked when the end of a media file has been reached during playback.
        void  setOnErrorListener(OnErrorListener l)
Register a callback to be invoked when an error has happened during an asynchronous operation.
        void  setOnPreparedListener(OnPreparedListener l)
Register a callback to be invoked when the media file is ready for playback.
        void  setWakeMode(Context context, int mode)
Set the power management behavior for this MediaPlayer.
        void  start()
Starts or resumes playback.
        void  stop()
Stops playback.

Protected Methods

        void  finalize()
Called by the virtual machine when there are no longer any (non-weak) references to the receiver.
Methods inherited from class java.lang.Object

Details

Public Constructors

public MediaPlayer()

Public Methods

public static MediaPlayer create(Context context, Uri uri)

Convenience method to create a MediaPlayer for a given Uri.

Parameters

context The Context to use
uri The Uri from which to get the datasource

Returns

  • A MediaPlayer object, or null if creation failed. prepare() will already have been called.

public static MediaPlayer create(Context context, int resid)

Convenience method to create a MediaPlayer for a given resource id

Parameters

context The Context to use
resid The raw resource id (R.raw.) for the resource to use as the datasource

Returns

  • A MediaPlayer object, or null if creation failed. prepare() will already have been called.

public static MediaPlayer create(Context context, Uri uri, Surface surface)

Convenience method to create a MediaPlayer for a given Uri.

Parameters

context The Context to use
uri The Uri from which to get the datasource
surface The Surface to use for displaying the video

Returns

  • A MediaPlayer object, or null if creation failed. prepare() will already have been called.

public int getCurrentPosition()

Gets the current playback position.

Returns

  • The current position in milliseconds.

public int getDuration()

Gets the duration of the file.

Returns

  • The duration in milliseconds.

public Bitmap getFrameAt(int msec)

Returns a Bitmap containing the video frame at the specified time.

Parameters

msec The time to capture the video frame at, in milliseconds

Returns

  • A Bitmap containing the video frame at the specified time

public boolean isPlaying()

Returns true when the MediaPlayer is currently playing, and false if it hasn't been started or is paused.

Returns

  • True if currently playing, false otherwise

public void pause()

Pauses playback. Call start() to resume.

public void prepare()

Prepares the player for playback, synchronously. After setting the datasource and the display surface, you need to either call prepare() or prepareAsync(). For files, it is OK to call prepare(), which blocks until MediaPlayer is ready for playback.

Throws

IOException

public void prepareAsync()

Prepares the player for playback, asynchronously. After setting the datasource and the display surface, you need to either call prepare() or prepareAsync(). For streams, you should call prepareAsync(), which returns immediately, rather than blocking until enough data has been buffered.

public void release()

Releases resources associated with this MediaPlayer object. It is considered good practice to call this method when you're done using the MediaPlayer.

public void reset()

Resets the MediaPlayer to its uninitialized state. After calling this method, you will have to initialize it again by setting the datasource and calling prepare().

public void seekTo(int msec)

Seeks to specified position.

Parameters

msec The offset in milliseconds from the start to seek to.

public void setAudioStreamType(int streamtype)

Sets the audio stream type for this MediaPlayer. See AudioSystem for a list of stream types.

Parameters

streamtype The audio stream type.

public void setDataSource(String path)

Sets the data source (file-path or URL) to use.

Parameters

path The path of the file, or the URL of the stream you want to play.

public void setDataSource(FileDescriptor fd, String mimeType)

Sets the data source (FileDescriptor) to use.

Parameters

fd The FileDescriptor for the file you want to play.
mimeType content type of the file.

public void setDisplay(Surface sv)

Sets the Surface to use for displaying the video portion of the media. This call is optional. Not calling it when playing back a video will result in only the audio track being played.

public void setLooping(int looping)

Sets the player to be looping or non-looping.

Parameters

looping Pass 0 for non-looping, 1 for looping.

public void setOnBufferingUpdateListener(OnBufferingUpdateListener l)

Register a callback to be invoked when the status of a network stream's buffer has changed.

Parameters

l The callback that will be run

public void setOnCompletionListener(OnCompletionListener l)

Register a callback to be invoked when the end of a media file has been reached during playback.

Parameters

l The callback that will be run

public void setOnErrorListener(OnErrorListener l)

Register a callback to be invoked when an error has happened during an asynchronous operation.

Parameters

l The callback that will be run

public void setOnPreparedListener(OnPreparedListener l)

Register a callback to be invoked when the media file is ready for playback.

Parameters

l The callback that will be run

public void setWakeMode(Context context, int mode)

Set the power management behavior for this MediaPlayer. The parameter is a combination of PowerManager wake flags. By default, no attempt is made to keep the device awake during playback.

Parameters

context The Context to use.

public void start()

Starts or resumes playback. If playback had previously been paused, playback will continue from where it was paused. If playback had been stopped, or never started before, playback will start at the beginning.

public void stop()

Stops playback.

Protected Methods

protected void finalize()

Called by the virtual machine when there are no longer any (non-weak) references to the receiver. Subclasses can use this facility to guarantee that any associated resources are cleaned up before the receiver is garbage collected. Uncaught exceptions which are thrown during the running of the method cause it to terminate immediately, but are otherwise ignored.

Note: The virtual machine assumes that the implementation in class Object is empty.

Build m5-rc15g - 14 May 2008 12:50