android.media.MediaPlayer
getCurrentPosition()
, getDuration()
, getVideoHeight()
, getVideoWidth()
, setAudioStreamType(int)
,setLooping(boolean)
, setVolume(float, float)
, pause()
, start()
, stop()
, seekTo(int)
, prepare()
or
prepareAsync()
方法都是错误的用法。在一个 MediaPlayer 对象刚被创建之后,就马上调用这些方法中的任意一个或多个,那么提供给用户的 OnErrorListener.onError() 回调方法是不会被内部的播放引擎调用的,且对象状态保持不变。但是如果在调用 reset() 方法后,就马上调用这些方法中的一个或多个,OnErrorListener.onError() 回调方法就会被内部的播放引擎调用,且对象会转换到 Error 状态。
prepare()
,
prepareAsync()
, 或任意一种重载的
setDataSource
方法,在进行一些非常操作时会抛出
IllegalStateException 错误。
setDataSource(FileDescriptor)
, or
setDataSource(String)
, or
setDataSource(Context, Uri)
, or
setDataSource(FileDescriptor, long, long)
方法会使一个 MediaPlayer 对象从 Idle 状态转换到 Initialized 状态。
prepare()
or
prepareAsync()
都抛出 IllegalStateException 错误;
如果应用程序需要对内部状态的更新或在播放时出错的信息进行监听捕获,那么就可以通过注册适当的监听器来实现,下面是一些常用的注册监听器的方法
via calls to
setOnPreparedListener(OnPreparedListener)
setOnPreparedListener,
setOnVideoSizeChangedListener(OnVideoSizeChangedListener)
setOnVideoSizeChangedListener,
setOnSeekCompleteListener(OnSeekCompleteListener)
setOnSeekCompleteListener,
setOnCompletionListener(OnCompletionListener)
setOnCompletionListener,
setOnBufferingUpdateListener(OnBufferingUpdateListener)
setOnBufferingUpdateListener,
setOnInfoListener(OnInfoListener)
setOnInfoListener,
setOnErrorListener(OnErrorListener)
setOnErrorListener, etc).
In order to receive the respective callback associated with these listeners, applications are required to create MediaPlayer objects on a thread with its own Looper running (main UI thread by default has a Looper running).
Nested Classes(内部类) | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
interface
|
MediaPlayer.OnBufferingUpdateListener | 定义了当从网络上接收 media source 时的缓冲状态的回调 | |||||||||
interface
|
MediaPlayer.OnCompletionListener | 定义了当 media source 播放完成时的回调 | |||||||||
interface
|
MediaPlayer.OnErrorListener | 定义了当进行异步操作出错时的回调(其它错误将会在方法调用时就抛出) | |||||||||
interface
|
MediaPlayer.OnInfoListener | 定义了一些通信信息和媒体播放过程中出现警告时的回调 | |||||||||
interface
|
MediaPlayer.OnPreparedListener | 当 media source 准备好播放时回调 | |||||||||
interface
|
MediaPlayer.OnSeekCompleteListener | 当 seek 操作完成时的回调 | |||||||||
interface
|
MediaPlayer.OnTimedTextListener | 当一个定时的文本可用于显示时的回调 | |||||||||
interface
|
MediaPlayer.OnVideoSizeChangedListener | 当 video size 第一次被知道或更新时的回调 | |||||||||
class
|
MediaPlayer.TrackInfo | 一个用于给 MediaPlayer 返回每个 audio/video/subtitle track's 的元信息的类 |
Constants(常量) | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | MEDIA_ERROR_IO | 文件或网络相关操作出错 | |||||||||
int | MEDIA_ERROR_MALFORMED | Bitstream 与相关的编码标准或具体文件不相匹配 | |||||||||
int | MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK | video 已经被转换成流且其容器不支持 progressive 播放,如 video 的下标没有从文件的开始处算起 | |||||||||
int | MEDIA_ERROR_SERVER_DIED | Media server 挂掉了 | |||||||||
int | MEDIA_ERROR_TIMED_OUT | 一些操作长时间没完成(超时),通常是3-5秒 | |||||||||
int | MEDIA_ERROR_UNKNOWN | 一些未定义的 media 播放器错误 | |||||||||
int | MEDIA_ERROR_UNSUPPORTED | Bitstream 与相关的编码标准相符合,但是 media 框架并不支持其特性要求 | |||||||||
int | MEDIA_INFO_BAD_INTERLEAVING | 交错出错意味着交错存储的 media 有错误或者根本没交错 | |||||||||
int | MEDIA_INFO_BUFFERING_END | MediaPlayer is resuming playback after filling buffers. | |||||||||
int | MEDIA_INFO_BUFFERING_START | 为了缓冲更多的数据,MediaPlayer 暂停播放 | |||||||||
int | MEDIA_INFO_METADATA_UPDATE | 一个新的元数据集可用 | |||||||||
int | MEDIA_INFO_NOT_SEEKABLE | media 无法定位播放(如直播) | |||||||||
int | MEDIA_INFO_SUBTITLE_TIMED_OUT | 读取字幕 track 超时 | |||||||||
int | MEDIA_INFO_UNKNOWN | 未定义的 media player 信息 | |||||||||
int | MEDIA_INFO_UNSUPPORTED_SUBTITLE | media framework 不支持 subtitle track | |||||||||
int | MEDIA_INFO_VIDEO_RENDERING_START | The player just pushed the very first video frame for rendering. | |||||||||
int | MEDIA_INFO_VIDEO_TRACK_LAGGING | video 太复杂,以致于 decoder 无法解码,即 decode 帧的速度太慢 | |||||||||
String | MEDIA_MIMETYPE_TEXT_SUBRIP | MIME type for SubRip (SRT) container. | |||||||||
int | VIDEO_SCALING_MODE_SCALE_TO_FIT | Specifies a video scaling mode. | |||||||||
int | VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING | Specifies a video scaling mode. |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
MediaPlayer()
默认的构造方法
|
Public Methods(公有方法) | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
void
|
addTimedTextSource(Context context, Uri uri, String mimeType)
Adds an external timed text source file (Uri).增加一个外部的 timed text 源文件(Uri)
|
||||||||||
void
|
addTimedTextSource(String path, String mimeType)
增加一个外部的 timed text 源文件
|
||||||||||
void
|
addTimedTextSource(FileDescriptor fd, long offset, long length, String mimeType)
增加一个外部的 timed text 源文件 (FileDescriptor).
|
||||||||||
void
|
addTimedTextSource(FileDescriptor fd, String mimeType)
增加一个外部的 timed text 源文件(FileDescriptor).
|
||||||||||
void
|
attachAuxEffect(int effectId)
Attaches an auxiliary effect to the player.
|
||||||||||
static MediaPlayer
|
create(Context context, Uri uri, SurfaceHolder holder)
为指定的 Uri 创建一个 MediaPlayer 对象
|
||||||||||
static MediaPlayer
|
create(Context context, int resid)
为指定的 resource id 创建一个MediaPlayer 对象
|
||||||||||
static MediaPlayer
|
create(Context context, Uri uri)
为指定的 Uri 创建一个 MediaPlayer 对象
|
||||||||||
void
|
deselectTrack(int index)
取消一个 track
|
||||||||||
int
|
getAudioSessionId()
返回 audio session 的 ID
|
||||||||||
int
|
getCurrentPosition()
返回当前播放的位置
|
||||||||||
int
|
getDuration()
返回 file 的时长
|
||||||||||
TrackInfo[]
|
getTrackInfo()
通过一个数组返回一个 track 的信息
|
||||||||||
int
|
getVideoHeight()
Returns the height of the video.
|
||||||||||
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.
|
||||||||||
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 time position.
|
||||||||||
void
|
selectTrack(int index)
Selects a track.
|
||||||||||
void
|
setAudioSessionId(int sessionId)
Sets the audio session ID.
|
||||||||||
void
|
setAudioStreamType(int streamtype)
Sets the audio stream type for this MediaPlayer.
|
||||||||||
void
|
setAuxEffectSendLevel(float level)
Sets the send level of the player to the attached auxiliary effect .
|
||||||||||
void
|
setDataSource(String path)
Sets the data source (file-path or http/rtsp URL) to use.
|
||||||||||
void
|
setDataSource(Context context, Uri uri, Map<String, String> headers)
Sets the data source as a content Uri.
|
||||||||||
void
|
setDataSource(Context context, Uri uri)
Sets the data source as a content Uri.
|
||||||||||
void
|
setDataSource(FileDescriptor fd, long offset, long length)
Sets the data source (FileDescriptor) to use.
|
||||||||||
void
|
setDataSource(FileDescriptor fd)
Sets the data source (FileDescriptor) to use.
|
||||||||||
void
|
setDisplay(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
|
setNextMediaPlayer(MediaPlayer next)
Set the MediaPlayer to start when this MediaPlayer finishes playback (i.e.
|
||||||||||
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
|
setOnInfoListener(MediaPlayer.OnInfoListener listener)
Register a callback to be invoked when an info/warning is available.
|
||||||||||
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 is available for 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(Surface surface)
Sets the
Surface to be used as the sink for the video portion of the media.
|
||||||||||
void
|
setVideoScalingMode(int mode)
Sets video scaling mode.
|
||||||||||
void
|
setVolume(float leftVolume, float rightVolume)
设置播放声音
|
||||||||||
void
|
setWakeMode(Context context, int mode)
Set the low-level power management behavior for this MediaPlayer.
|
||||||||||
void
|
start()
开始或恢复播放
|
||||||||||
void
|
stop()
停止播放
|
Protected Methods(保护方法) | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
void
|
finalize()
当垃圾回收器发现某个对象不可达时,调用此方法以回收该对象的内存
|