public class MediaPlayer extends Object
| 限定符和类型 | 类和说明 |
|---|---|
static interface |
MediaPlayer.OnBufferingUpdateListener |
static interface |
MediaPlayer.OnCompletionListener |
static interface |
MediaPlayer.OnErrorListener |
static interface |
MediaPlayer.OnFreshVideo |
static interface |
MediaPlayer.OnHWRenderFailedListener |
static interface |
MediaPlayer.OnInfoListener |
static interface |
MediaPlayer.OnPreparedListener |
static interface |
MediaPlayer.OnSeekCompleteListener |
static interface |
MediaPlayer.OnTimedTextListener |
static interface |
MediaPlayer.OnVideoSizeChangedListener |
static class |
MediaPlayer.TrackInfo
Class for MediaPlayer to return each audio/video/subtitle track's metadata.
|
| 限定符和类型 | 字段和说明 |
|---|---|
static int |
KEY_PARAMETER_USEHWCODEC |
| 构造器和说明 |
|---|
MediaPlayer(android.content.Context ctx)
Default constructor.
|
MediaPlayer(android.content.Context ctx,
boolean preferHWDecoder) |
| 限定符和类型 | 方法和说明 |
|---|---|
void |
addTimedTextSource(String path)
Set the file-path of an external timed text.
|
void |
deselectTrack(int index)
Deselect a track.
|
protected void |
finalize() |
android.util.SparseArray<MediaFormat> |
findTrackFromTrackInfo(int mediaTrackType,
MediaPlayer.TrackInfo[] trackInfo) |
int |
getAudioTrack()
Get the audio track number in playback
|
int |
getCurrentPosition()
Gets the current playback position.
|
int |
getDuration()
Gets the duration of the file.
|
Metadata |
getMetadata()
Gets the media metadata.
|
String |
getMetaEncoding()
Get the encoding if haven't set with
setMetaEncoding(String) |
int |
getTimedTextLocation() |
String |
getTimedTextPath()
You can get the file-path of the external subtitle in use.
|
int |
getTimedTextTrack()
Get the subtitle track number in playback
|
MediaPlayer.TrackInfo[] |
getTrackInfo()
Use default chartset
getTrackInfo() method. |
MediaPlayer.TrackInfo[] |
getTrackInfo(String encoding)
Returns an array of track information.
|
int |
getVideoHeight()
Returns the height of the video.
|
int |
getVideoTrack()
Get the video track number in playback
|
int |
getVideoWidth()
Returns the width of the video.
|
boolean |
isLooping()
Checks whether the MediaPlayer is looping or non-looping.
|
boolean |
isPlaying()
Checks whether the MediaPlayer is playing.
|
int |
native_draw_frame() |
int |
native_get_current_position() |
int |
native_get_duration() |
int |
native_get_video_height() |
int |
native_get_video_width() |
int |
native_is_playing() |
int |
native_pause() |
int |
native_prepare_async() |
int |
native_prepare() |
void |
native_release_surface() |
int |
native_release() |
int |
native_reset() |
int |
native_seekTo(int msec) |
int |
native_set_audio_effect(int t) |
void |
native_set_datasource(FileDescriptor fileDescriptor,
long offset,
long length)
Sets the data source (FileDescriptor) to use.
|
void |
native_set_datasource(String path,
String[] keys,
String[] values)
Sets the data source (file-path or http/rtsp/mms URL) to use.
|
int |
native_set_parameter(int cmd,
long arg1,
long arg2) |
void |
native_set_video_surface(android.view.Surface surface) |
int |
native_setup(Object thiz) |
int |
native_start() |
int |
native_stop() |
int |
native_surface_change(int w,
int h) |
int |
native_surface_create() |
int |
onDrawFrame() |
int |
onSurfaceChanged(int w,
int h) |
int |
onSurfaceCreated() |
void |
pause()
The same as
pause() |
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 |
releaseDisplay()
Calling this result in only the audio track being played.
|
void |
reset()
Resets the MediaPlayer to its uninitialized state.
|
void |
seekTo(int msec)
Seeks to specified time position.
|
void |
selectTrack(int index)
Selects a track.
|
int |
setAuxEffectSendLevel(int type)
audio effect API
|
void |
setDataSource(android.content.Context context,
android.net.Uri uri)
Sets the data source as a content Uri.
|
void |
setDataSource(android.content.Context context,
android.net.Uri uri,
Map<String,String> headers) |
void |
setDataSource(FileDescriptor fd)
Sets the data source (FileDescriptor) to use.
|
void |
setDataSource(FileDescriptor fd,
long offset,
long length)
Sets the data source (FileDescriptor) to use.
|
void |
setDataSource(String path)
Sets the data source (file-path or http/rtsp URL) to use.
|
void |
setDataSource(String path,
Map<String,String> headers)
Sets the data source (file-path or http/rtsp URL) to use.
|
void |
setDataSource(String path,
String[] keys,
String[] values)
Sets the data source (file-path or http/rtsp URL) to use.
|
void |
setDisplay(android.view.SurfaceHolder sh)
Sets the SurfaceHolder to use for displaying the video portion of the
media.
|
void |
setLooping(boolean looping)
Sets the player to be looping or non-looping.
|
void |
setMetaEncoding(String encoding)
Set the encoding MediaPlayer will use to determine the metadata
|
void |
setOnBufferingUpdateListener(MediaPlayer.OnBufferingUpdateListener listener)
Register a callback to be invoked when the status of a network stream's
buffer has changed.
|
void |
setOnCompletionListener(MediaPlayer.OnCompletionListener listener)
Register a callback to be invoked when the end of a media source has been
reached during playback.
|
void |
setOnErrorListener(MediaPlayer.OnErrorListener listener)
Register a callback to be invoked when an error has happened during an
asynchronous operation.
|
void |
setOnFreshVideo(MediaPlayer.OnFreshVideo listener) |
void |
setOnInfoListener(MediaPlayer.OnInfoListener listener) |
void |
setOnPreparedListener(MediaPlayer.OnPreparedListener listener)
Register a callback to be invoked when the media source is ready for
playback.
|
void |
setOnSeekCompleteListener(MediaPlayer.OnSeekCompleteListener listener)
Register a callback to be invoked when a seek operation has been
completed.
|
void |
setOnTimedTextListener(MediaPlayer.OnTimedTextListener listener)
Register a callback to be invoked when a timed text need to display.
|
void |
setOnVideoSizeChangedListener(MediaPlayer.OnVideoSizeChangedListener listener)
Register a callback to be invoked when the video size is known or
updated.
|
void |
setScreenOnWhilePlaying(boolean screenOn)
Control whether we should use the attached SurfaceHolder to keep the screen
on while video playback is occurring.
|
void |
setSurface(android.view.Surface surface)
Sets the Surface to use for displaying the video portion of the media.
|
void |
setTimedTextEncoding(String encoding)
Set the encoding to display timed text.
|
void |
setTimedTextShown(boolean shown)
Tell the MediaPlayer whether to show timed text
|
void |
setVolume(float leftVolume,
float rightVolume) |
void |
setWakeMode(android.content.Context context,
int mode)
Set the low-level power management behavior for this MediaPlayer.
|
void |
start()
Starts or resumes playback.
|
void |
stop()
The same as
stop() |
public static final int KEY_PARAMETER_USEHWCODEC
public MediaPlayer(android.content.Context ctx)
When done with the MediaPlayer, you should call release(), to free
the resources. If not released, too many MediaPlayer instances may result
in an exception.
public MediaPlayer(android.content.Context ctx,
boolean preferHWDecoder)
public void native_set_video_surface(android.view.Surface surface)
public void setDisplay(android.view.SurfaceHolder sh)
sh - the SurfaceHolder to use for video displaypublic void setSurface(android.view.Surface surface)
setDisplay(SurfaceHolder).surface - the Surface to use for video displaypublic void setDataSource(String path) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException
path - the path of the file, or the http/rtsp URL of the stream you want
to playIllegalStateException - if it is called in an invalid state
When path refers to a local file, the file may
actually be opened by a process other than the calling
application. This implies that the pathname should be an absolute
path (as any other process runs with unspecified current working
directory), and that the pathname should reference a
world-readable file. As an alternative, the application could
first open the file for reading, and then use the file descriptor
form setDataSource(FileDescriptor).IOExceptionIllegalArgumentExceptionSecurityExceptionpublic void setDataSource(android.content.Context context,
android.net.Uri uri)
throws IOException,
IllegalArgumentException,
SecurityException,
IllegalStateException
context - the Context to use when resolving the Uriuri - the Content URI of the data you want to playIllegalStateException - if it is called in an invalid stateIOExceptionIllegalArgumentExceptionSecurityExceptionpublic void setDataSource(android.content.Context context,
android.net.Uri uri,
Map<String,String> headers)
throws IOException,
IllegalArgumentException,
SecurityException,
IllegalStateException
public void setDataSource(String path, Map<String,String> headers) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException
path - the path of the file, or the http/rtsp URL of the stream you want to playheaders - the headers associated with the http request for the stream you want to playIllegalStateException - if it is called in an invalid stateIOExceptionIllegalArgumentExceptionSecurityExceptionpublic void setDataSource(String path, String[] keys, String[] values) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException
path - the path of the file, or the http/rtsp URL of the stream you want to playkeys - AVOption keyvalues - AVOption valueIllegalStateException - if it is called in an invalid stateIOExceptionIllegalArgumentExceptionSecurityExceptionpublic void native_set_datasource(String path, String[] keys, String[] values) throws IOException, IllegalArgumentException, IllegalStateException
path - the path of the file, or the http/rtsp/mms URL of the stream you
want to playkeys - AVOption keyvalues - AVOption valueIllegalStateException - if it is called in an invalid stateIOExceptionIllegalArgumentExceptionpublic void setDataSource(FileDescriptor fd) throws IOException, IllegalArgumentException, IllegalStateException
fd - the FileDescriptor for the file you want to playIllegalStateException - if it is called in an invalid stateIOExceptionIllegalArgumentExceptionpublic void setDataSource(FileDescriptor fd, long offset, long length) throws IOException, IllegalArgumentException, IllegalStateException
fd - the FileDescriptor for the file you want to playoffset - the offset into the file where the data to be played starts, in byteslength - the length in bytes of the data to be playedIllegalStateException - if it is called in an invalid stateIOExceptionIllegalArgumentExceptionpublic void native_set_datasource(FileDescriptor fileDescriptor, long offset, long length) throws IOException, IllegalArgumentException, IllegalStateException
fileDescriptor - the FileDescriptor for the file you want to playIllegalStateException - if it is called in an invalid stateIOExceptionIllegalArgumentExceptionpublic void prepare()
throws IllegalStateException,
IOException
IllegalStateException - if it is called in an invalid stateIOExceptionpublic int native_prepare()
throws IllegalStateException
public void prepareAsync()
throws IllegalStateException,
IOException
IllegalStateException - if it is called in an invalid stateIOExceptionpublic int native_prepare_async()
throws IllegalStateException
public void start()
throws IllegalStateException
IllegalStateException - if it is called in an invalid statepublic int native_start()
throws IllegalStateException
public void stop()
throws IllegalStateException
stop()IllegalStateException - if the internal player engine has not been initialized.public int native_stop()
throws IllegalStateException
public void pause()
throws IllegalStateException
pause()IllegalStateException - if the internal player engine has not been initialized.public int native_pause()
throws IllegalStateException
public int setAuxEffectSendLevel(int type)
type - public void setWakeMode(android.content.Context context,
int mode)
setDisplay(SurfaceHolder) and thus can use the high-level
setScreenOnWhilePlaying(boolean) feature.
This function has the MediaPlayer access the low-level power manager
service to control the device's power usage while playing is occurring. The
parameter is a combination of PowerManager wake flags.
Use of this method requires Manifest.permission.WAKE_LOCK
permission. By default, no attempt is made to keep the device awake during
playback.context - the Context to usemode - the power/wake mode to setPowerManagerpublic void setScreenOnWhilePlaying(boolean screenOn)
setWakeMode(android.content.Context, int) where possible, since it doesn't require that the
application have permission for low-level wake lock access.screenOn - Supply true to keep the screen on, false to allow it to turn off.public int getVideoWidth()
setOnVideoSizeChangedListener(OnVideoSizeChangedListener)
to provide a notification when the width is available.public int native_get_video_width()
public int getVideoHeight()
setOnVideoSizeChangedListener(OnVideoSizeChangedListener)
to provide a notification when the height is available.public int native_get_video_height()
public boolean isPlaying()
public void seekTo(int msec)
msec - the offset in milliseconds from the start to seek toIllegalStateException - if the internal player engine has not been initializedpublic int native_seekTo(int msec)
throws IllegalStateException
public int getCurrentPosition()
public int native_get_current_position()
public int getDuration()
public int native_get_duration()
public Metadata getMetadata()
public void release()
public int native_release()
public void reset()
public int native_reset()
public void setLooping(boolean looping)
looping - whether to loop or notpublic boolean isLooping()
public void setVolume(float leftVolume,
float rightVolume)
public MediaPlayer.TrackInfo[] getTrackInfo(String encoding)
public MediaPlayer.TrackInfo[] getTrackInfo()
getTrackInfo() method.MediaPlayer.TrackInfopublic android.util.SparseArray<MediaFormat> findTrackFromTrackInfo(int mediaTrackType, MediaPlayer.TrackInfo[] trackInfo)
mediaTrackType - trackInfo - MediaPlayer.TrackInfo.getTrackInfoArray()public void addTimedTextSource(String path)
path - must be a local filepublic void selectTrack(int index)
In any valid state, if it is called multiple times on the same type of track (ie. Video, Audio, Timed Text), the most recent one will be chosen.
The first audio and video tracks are selected by default if available, even though this method is not called. However, no timed text track will be selected until this function is called.
index - the index of the track to be selected. The valid range of the
index is 0..total number of track - 1. The total number of tracks
as well as the type of each individual track can be found by
calling getTrackInfo() method.getTrackInfo(java.lang.String)public void deselectTrack(int index)
Currently, the track must be a timed text track and no audio or video tracks can be deselected.
index - the index of the track to be deselected. The valid range of the
index is 0..total number of tracks - 1. The total number of tracks
as well as the type of each individual track can be found by
calling getTrackInfo() method.getTrackInfo(java.lang.String)public void setOnPreparedListener(MediaPlayer.OnPreparedListener listener)
listener - the callback that will be runpublic void setOnFreshVideo(MediaPlayer.OnFreshVideo listener)
public void setOnCompletionListener(MediaPlayer.OnCompletionListener listener)
listener - the callback that will be runpublic void setOnBufferingUpdateListener(MediaPlayer.OnBufferingUpdateListener listener)
listener - the callback that will be run.public void setOnSeekCompleteListener(MediaPlayer.OnSeekCompleteListener listener)
listener - the callback that will be runpublic void setOnVideoSizeChangedListener(MediaPlayer.OnVideoSizeChangedListener listener)
listener - the callback that will be runpublic void setOnErrorListener(MediaPlayer.OnErrorListener listener)
listener - the callback that will be runpublic void setOnInfoListener(MediaPlayer.OnInfoListener listener)
public void setOnTimedTextListener(MediaPlayer.OnTimedTextListener listener)
listener - the callback that will be runpublic void releaseDisplay()
public void native_release_surface()
public String getMetaEncoding()
setMetaEncoding(String)public void setMetaEncoding(String encoding)
encoding - e.g. "UTF-8"public int getAudioTrack()
public int getVideoTrack()
public void setTimedTextShown(boolean shown)
shown - true if wanna showpublic void setTimedTextEncoding(String encoding)
encoding - MediaPlayer will detet it if nullpublic int getTimedTextLocation()
public String getTimedTextPath()
public int getTimedTextTrack()
public int onSurfaceCreated()
public int onSurfaceChanged(int w,
int h)
public int onDrawFrame()
public int native_setup(Object thiz)
public int native_is_playing()
public int native_set_parameter(int cmd,
long arg1,
long arg2)
public int native_surface_create()
public int native_surface_change(int w,
int h)
public int native_draw_frame()
public int native_set_audio_effect(int t)