android.media
public
class
android.media.MediaPlayer
The MediaPlayer class is used for playing audio and video files and streams.
Nested Classes
Summary
Public Constructors
Public Methods
Protected Methods
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait
Details
Public Constructors
Public Methods
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.
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.
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.
|
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
|
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.