目錄: (3)Android 官方網站 對 MediaPlayer的介紹 正文: Android 官方網站 對 MediaPlayer的介紹 MediaPlayer public class MediaPlayer extends Object implements VolumeAutomation ...
目錄:
(3)Android 官方網站 對 MediaPlayer的介紹
正文:
MediaPlayer public class MediaPlayer extends Object implements VolumeAutomation, AudioRouting MediaPlayer類被用來控制音/視頻文件和流的播放。可以在VideoView中找到有關如何使用此類中的方法的示例。 這裡涉及的主題是: 1. 狀態圖 2. 有效和無效狀態 3. 許可權 4. 註冊信息和錯誤回調 ★ 開發者指南 有關如何使用MediaPlayer的更多信息,請閱讀Media Playback開發人員指南。 狀態圖 音/視頻文件和流的播放控制是作為一個狀態機來進行管理。下圖顯示了受支持的播放控制操作驅動的MediaPlayer對象的生命周期和狀態。 橢圓表示MediaPlayer對象可能駐留的狀態。弧表示驅動對象狀態轉換的播放控制操作。有兩種類型的弧。 單箭頭的弧表示同步方法調用,而雙箭頭的弧表示非同步方法調用。![](https://images2018.cnblogs.com/blog/323474/201807/323474-20180713213740363-253825688.gif)
- 當使用 new 創建MediaPlayer對象或者在調用 reset() 之後,它處於空閑狀態; 並且在調用 release() 之後,它處於 End 狀態。 在這兩個狀態之間是MediaPlayer對象的生命周期。
- 通常,一些播放控制操作可能由於各種原因而失敗,例如不支持的音頻/視頻格式,交錯的音頻/視頻,解析度太高,流超時等。因此,在這些情況下,關註錯誤報告和恢復是非常重要的。有時,由於編程錯誤,也可能在無效狀態下調用播放控制操作。在所有這些錯誤條件下,如果開發者事先通過setOnErrorListener(android.media.MediaPlayer.OnErrorListener)註冊了 OnErrorListener ,則內部播放器引擎會調用開發者提供的 OnErrorListener.onError() 方法。
- 調用 setDataSource(FileDescriptor), 或 setDataSource(String), 或 setDataSource(Context, Uri), 或 setDataSource(FileDescriptor, long, long), 或 setDataSource(MediaDataSource)
- 在開始播放之前,MediaPlayer 對象必須先進入準備狀態。
- 要開始播放,必須調用 start() ,start() 返回成功後,MediaPlayer對象則處於 Started狀態(Started state)。isPlaying()可用來測試 MediaPlayer對象是否處於 Started狀態(Started state)。
- 播放可以暫停和停止,並可以調整當前播放位置。 可以通過pause()暫停播放。 當對pause()的調用返回時,MediaPlayer對象進入Paused狀態(Pausedstate)。 請註意,從“已啟動”狀態(Started state)到“暫停”狀態(Paused state)的轉換(反之亦然)在播放器引擎中非同步發生。 在調用isPlaying()時更新狀態可能需要一些時間,對於流內容,它可能需要幾秒鐘。
- 調用stop()會停止播放並導致處於Started,Paused,Prepared或PlaybackCompleted狀態(state)的MediaPlayer進入Stopped狀態(Stopped state)。
- 可以通過調用seekTo(long, int)調整播放位置
- 當播放到達流的結尾時,播放完成。
Method Name | Valid Sates | Invalid States | Comments |
attachAuxEffect | {Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted} | {Idle, Error} | This method must be called after setDataSource. Calling it does not change the object state. |
getAudioSessionId | any | {} | This method can be called in any state and calling it does not change the object state. |
getCurrentPosition | {Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted} | {Error} | Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state. |
getDuration | {Prepared, Started, Paused, Stopped, PlaybackCompleted} | {Idle, Initialized, Error} | Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state. |
getVideoHeight | {Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted} | {Error} | Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state. |
getVideoWidth | {Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted} | {Error} | Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state. |
isPlaying | {Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted} | {Error} | Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state. |
pause | {Started, Paused, PlaybackCompleted} | {Idle, Initialized, Prepared, Stopped, Error} | Successful invoke of this method in a valid state transfers the object to the Paused state. Calling this method in an invalid state transfers the object to the Error state. |
prepare | {Initialized, Stopped} | {Idle, Prepared, Started, Paused, PlaybackCompleted, Error} | Successful invoke of this method in a valid state transfers the object to the Prepared state. Calling this method in an invalid state throws an IllegalStateException. |
prepareAsync | {Initialized, Stopped} | {Idle, Prepared, Started, Paused, PlaybackCompleted, Error} | Successful invoke of this method in a valid state transfers the object to the Preparing state. Calling this method in an invalid state throws an IllegalStateException. |
release | any | {} | After release(), the object is no longer available. |
reset | {Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, Error} | {} | After reset(), the object is like being just created. |
seekTo | {Prepared, Started, Paused, PlaybackCompleted} | {Idle, Initialized, Stopped, Error} | Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state. |
setAudioAttributes | {Idle, Initialized, Stopped, Prepared, Started, Paused, PlaybackCompleted} | {Error} | Successful invoke of this method does not change the state. In order for the target audio attributes type to become effective, this method must be called before prepare() or prepareAsync(). |
setAudioSessionId | {Idle} | {Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, Error} | This method must be called in idle state as the audio session ID must be known before calling setDataSource. Calling it does not change the object state. |
setAudioStreamType (deprecated) | {Idle, Initialized, Stopped, Prepared, Started, Paused, PlaybackCompleted} | {Error} | Successful invoke of this method does not change the state. In order for the target audio stream type to become effective, this method must be called before prepare() or prepareAsync(). |
setAuxEffectSendLevel | any | {} | Calling this method does not change the object state. |
setDataSource | {Idle} | {Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, Error} | Successful invoke of this method in a valid state transfers the object to the Initialized state. Calling this method in an invalid state throws an IllegalStateException. |
setDisplay | any | {} | This method can be called in any state and calling it does not change the object state. |
setSurface | any | {} | This method can be called in any state and calling it does not change the object state. |
setVideoScalingMode | {Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted} | {Idle, Error} | Successful invoke of this method does not change the state. |
setLooping | {Idle, Initialized, Stopped, Prepared, Started, Paused, PlaybackCompleted} | {Error} | Successful invoke of this method in a valid state does not change the state. Calling this method in an invalid state transfers the object to the Error state. |
isLooping | any | {} | This method can be called in any state and calling it does not change the object state. |
setOnBufferingUpdateListener | any | {} | This method can be called in any state and calling it does not change the object state. |
setOnCompletionListener | any | {} | This method can be called in any state and calling it does not change the object state. |
setOnErrorListener | any | {} | This method can be called in any state and calling it does not change the object state. |
setOnPreparedListener | any | {} | This method can be called in any state and calling it does not change the object state. |
setOnSeekCompleteListener | any | {} | This method can be called in any state and calling it does not change the object state. |
setPlaybackParams | {Initialized, Prepared, Started, Paused, PlaybackCompleted, Error} | {Idle, Stopped} | This method will change state in some cases, depending on when it's called. |
setScreenOnWhilePlaying | any | {} | This method can be called in any state and calling it does not change the object state. |
setVolume | {Idle, Initialized, Stopped, Prepared, Started, Paused, PlaybackCompleted} | {Error} | Successful invoke of this method does not change the state. |
setWakeMode | any | {} | This method can be called in any state and calling it does not change the object state. |
start | {Prepared, Started, Paused, PlaybackCompleted} | {Idle, Initialized, Stopped, Error} | Successful invoke of this method in a valid state transfers the object to the Started state. Calling this method in an invalid state transfers the object to the Error state. |
stop | {Prepared, Started, Stopped, Paused, PlaybackCompleted} | {Idle, Initialized, Error} | Successful invoke of this method in a valid state transfers the object to the Stopped state. Calling this method in an invalid state transfers the object to the Error state. |
getTrackInfo | {Prepared, Started, Stopped, Paused, PlaybackCompleted} | {Idle, Initialized, Error} | Successful invoke of this method does not change the state. |
addTimedTextSource | {Prepared, Started, Stopped, Paused, PlaybackCompleted} | {Idle, Initialized, Error} | Successful invoke of this method does not change the state. |
selectTrack | {Prepared, Started, Stopped, Paused, PlaybackCompleted} | {Idle, Initialized, Error} | Successful invoke of this method does not change the state. |
deselectTrack | {Prepared, Started, Stopped, Paused, PlaybackCompleted} | {Idle, Initialized, Error} | Successful invoke of this method does not change the state. |