MediaPlayer 详解
来源:互联网 发布:上海巨人网络待遇 编辑:程序博客网 时间:2024/06/05 00:42
MediaPlayer
extends ObjectClass Overview
MediaPlayer class can be used to control playback of audio/video files and streams. An example on how to use the methods in this class can be found in VideoView
.
Topics covered here are:
- State Diagram
- Valid and Invalid States
- Permissions
- Register informational and error callbacks
Developer Guides
For more information about how to use MediaPlayer, read the Media Playback developer guide.
State Diagram
Playback control of audio/video files and streams is managed as a state machine. The following diagram shows the life cycle and the states of a MediaPlayer object driven by the supported playback control operations. The ovals represent the states a MediaPlayer object may reside in. The arcs represent the playback control operations that drive the object state transition. There are two types of arcs. The arcs with a single arrow head represent synchronous method calls, while those with a double arrow head represent asynchronous method calls.
翻译:
MediaPlayer 这个类用于控制视频、音频、流文件的播放控制。VideoView中有怎么使用这些方法的示例代码。
注意:上面的MediaPlayer的状态转换图很重要【越看越像数电中的状态机】
From this state diagram, one can see that a MediaPlayer object has the following states:
- When a MediaPlayer object is just created using
new
or afterreset()
is called, it is in the Idle state; and afterrelease()
is called, it is in the End state. Between these two states is the life cycle of the MediaPlayer object.- There is a subtle but important difference between a newly constructed MediaPlayer object and the MediaPlayer object after
reset()
is called. It is a programming error to invoke methods such asgetCurrentPosition()
,getDuration()
,getVideoHeight()
,getVideoWidth()
,setAudioStreamType(int)
,setLooping(boolean)
,setVolume(float,float)
,pause()
,start()
,stop()
,seekTo(int)
,prepare()
orprepareAsync()
in the Idle state for both cases. If any of these methods is called right after a MediaPlayer object is constructed, the user supplied callback method OnErrorListener.onError() won't be called by the internal player engine and the object state remains unchanged; but if these methods are called right afterreset()
, the user supplied callback method OnErrorListener.onError() will be invoked by the internal player engine and the object will be transfered to the Error state. - It is also recommended that once a MediaPlayer object is no longer being used, call
release()
immediately so that resources used by the internal player engine associated with the MediaPlayer object can be released immediately. Resource may include singleton resources such as hardware acceleration components and failure to callrelease()
may cause subsequent instances of MediaPlayer objects to fallback to software implementations or fail altogether. Once the MediaPlayer object is in the End state, it can no longer be used and there is no way to bring it back to any other state. - Furthermore, the MediaPlayer objects created using
new
is in the Idle state, while those created with one of the overloaded convenientcreate
methods are NOT in theIdle state. In fact, the objects are in the Prepared state if the creation usingcreate
method is successful.
- There is a subtle but important difference between a newly constructed MediaPlayer object and the MediaPlayer object after
- In general, some playback control operation may fail due to various reasons, such as unsupported audio/video format, poorly interleaved audio/video, resolution too high, streaming timeout, and the like. Thus, error reporting and recovery is an important concern under these circumstances. Sometimes, due to programming errors, invoking a playback control operation in an invalid state may also occur. Under all these error conditions, the internal player engine invokes a user supplied OnErrorListener.onError() method if an OnErrorListener has been registered beforehand via
setOnErrorListener(android.media.MediaPlayer.OnErrorListener)
.- It is important to note that once an error occurs, the MediaPlayer object enters the Error state (except as noted above), even if an error listener has not been registered by the application.
- In order to reuse a MediaPlayer object that is in the Error state and recover from the error,
reset()
can be called to restore the object to its Idle state. - It is good programming practice to have your application register a OnErrorListener to look out for error notifications from the internal player engine.
- IllegalStateException is thrown to prevent programming errors such as calling
prepare()
,prepareAsync()
, or one of the overloadedsetDataSource
methods in an invalid state.
getCurrentPosition()
, getDuration()
, getVideoHeight()
, getVideoWidth()
, setAudioStreamType(int)
, setLooping(boolean)
, setVolume(float,float)
, pause()
, start()
, stop()
, seekTo(int)
, prepare()
or prepareAsync() 都会报错
new 出来的,不会回调:
OnErrorListener.onError() ,状态保持不变。setOnErrorListener(android.media.MediaPlayer.OnErrorListener)
.】prepare()
, prepareAsync()
, 或者在不合适的状态下调用 setDataSource
,会抛出IllegalStateException,- Calling
setDataSource(FileDescriptor)
, orsetDataSource(String)
, orsetDataSource(Context, Uri)
, orsetDataSource(FileDescriptor, long, long)
transfers a MediaPlayer object in the Idle state to the Initialized state.- An IllegalStateException is thrown if setDataSource() is called in any other state.
- It is good programming practice to always look out for
IllegalArgumentException
andIOException
that may be thrown from the overloadedsetDataSource
methods.
- A MediaPlayer object must first enter the Prepared state before playback can be started.
- There are two ways (synchronous vs. asynchronous) that the Prepared state can be reached: either a call to
prepare()
(synchronous) which transfers the object to thePrepared state once the method call returns, or a call toprepareAsync()
(asynchronous) which first transfers the object to the Preparing state after the call returns (which occurs almost right way) while the internal player engine continues working on the rest of preparation work until the preparation work completes. When the preparation completes or whenprepare()
call returns, the internal player engine then calls a user supplied callback method, onPrepared() of the OnPreparedListener interface, if an OnPreparedListener is registered beforehand viasetOnPreparedListener(android.media.MediaPlayer.OnPreparedListener)
. - It is important to note that the Preparing state is a transient state, and the behavior of calling any method with side effect while a MediaPlayer object is in the Preparingstate is undefined.
- An IllegalStateException is thrown if
prepare()
orprepareAsync()
is called in any other state. - While in the Prepared state, properties such as audio/sound volume, screenOnWhilePlaying, looping can be adjusted by invoking the corresponding set methods.
- There are two ways (synchronous vs. asynchronous) that the Prepared state can be reached: either a call to
prepare()
(synchronous) , prepareAsync()
(asynchronous)。准备完成,setOnPreparedListener(android.media.MediaPlayer.OnPreparedListener)
.中的onPrepared()方法- To start the playback,
start()
must be called. Afterstart()
returns successfully, the MediaPlayer object is in the Started state.isPlaying()
can be called to test whether the MediaPlayer object is in the Started state.- While in the Started state, the internal player engine calls a user supplied OnBufferingUpdateListener.onBufferingUpdate() callback method if a OnBufferingUpdateListener has been registered beforehand via
setOnBufferingUpdateListener(OnBufferingUpdateListener)
. This callback allows applications to keep track of the buffering status while streaming audio/video. - Calling
start()
has not effect on a MediaPlayer object that is already in the Started state.
- While in the Started state, the internal player engine calls a user supplied OnBufferingUpdateListener.onBufferingUpdate() callback method if a OnBufferingUpdateListener has been registered beforehand via
- Playback can be paused and stopped, and the current playback position can be adjusted. Playback can be paused via
pause()
. When the call topause()
returns, the MediaPlayer object enters the Paused state. Note that the transition from the Started state to the Paused state and vice versa happens asynchronously in the player engine. It may take some time before the state is updated in calls toisPlaying()
, and it can be a number of seconds in the case of streamed content.- Calling
start()
to resume playback for a paused MediaPlayer object, and the resumed playback position is the same as where it was paused. When the call tostart()
returns, the paused MediaPlayer object goes back to the Started state. - Calling
pause()
has no effect on a MediaPlayer object that is already in the Paused state.
- Calling
- Calling
stop()
stops playback and causes a MediaPlayer in the Started, Paused, Prepared or PlaybackCompleted state to enter the Stopped state.- Once in the Stopped state, playback cannot be started until
prepare()
orprepareAsync()
are called to set the MediaPlayer object to the Prepared state again. - Calling
stop()
has no effect on a MediaPlayer object that is already in the Stopped state.
- Once in the Stopped state, playback cannot be started until
- The playback position can be adjusted with a call to
seekTo(int)
.- Although the asynchronuous
seekTo(int)
call returns right way, the actual seek operation may take a while to finish, especially for audio/video being streamed. When the actual seek operation completes, the internal player engine calls a user supplied OnSeekComplete.onSeekComplete() if an OnSeekCompleteListener has been registered beforehand viasetOnSeekCompleteListener(OnSeekCompleteListener)
. - Please note that
seekTo(int)
can also be called in the other states, such as Prepared, Paused and PlaybackCompleted state. - Furthermore, the actual current playback position can be retrieved with a call to
getCurrentPosition()
, which is helpful for applications such as a Music player that need to keep track of the playback progress.
- Although the asynchronuous
- When the playback reaches the end of stream, the playback completes.
- If the looping mode was being set to truewith
setLooping(boolean)
, the MediaPlayer object shall remain in the Started state. - If the looping mode was set to false , the player engine calls a user supplied callback method, OnCompletion.onCompletion(), if a OnCompletionListener is registered beforehand via
setOnCompletionListener(OnCompletionListener)
. The invoke of the callback signals that the object is now in the PlaybackCompleted state. - While in the PlaybackCompleted state, calling
start()
can restart the playback from the beginning of the audio/video source.
- If the looping mode was being set to truewith
getCurrentPosition()
setLooping(boolean)为true,播放
,结束了,播放器也会进入到 Started 状态Callbacks
Applications may want to register for informational and error events in order to be informed of some internal state update and possible runtime errors during playback or streaming. Registration for these events is done by properly setting the appropriate listeners (via calls tosetOnPreparedListener(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).
- MediaPlayer 详解
- mediaplayer 详解
- MediaPlayer详解
- MediaPlayer详解
- MediaPlayer详解
- Android之MediaPlayer详解
- Android MediaPlayer类详解
- Android MediaPlayer类详解
- android MediaPlayer详解
- Android MediaPlayer生命周期详解
- Android之MediaPlayer详解
- android MediaPlayer API 详解
- Android MediaPlayer详解
- Android MediaPlayer类详解
- mediaPlayer的详解
- MediaPlayer 生命周期详解
- Android之MediaPlayer详解
- Android mediaPlayer属性详解
- linux下使用tar命令
- ASCII码表
- linux内核启动过程学习总结
- java将HTML转换成TXT
- 在Android.mk中输出信息
- MediaPlayer 详解
- 戴尔今日正式摘牌 市值定格243.3亿美元
- 资深企业家和投资人王炳晋加盟金沙江创投
- B树的C++实现
- warning LNK4098: defaultlib "LIBCD" conflicts with use of other libs; use /N
- 机器学习
- Unable to execute dex: Multiple dex files define 解决方法
- List对象排序通用方法
- 画最小轮廓