目錄:
(3)Android 官方網站 對 MediaPlayer的介紹
正文:
Android 官方網站 對 MediaPlayer的介紹
MediaPlayer public class MediaPlayer extends Object implements VolumeAutomation, AudioRouting MediaPlayer類被用來控制音/視頻檔案和流的播放,可以在VideoView中找到有關如何使用此類中的方法的示例, 這里涉及的主題是: 1. 狀態圖 2. 有效和無效狀態 3. 權限 4. 注冊資訊和錯誤回呼 ★ 開發者指南 有關如何使用MediaPlayer的更多資訊,請閱讀Media Playback開發人員指南, 狀態圖 音/視頻檔案和流的播放控制是作為一個狀態機來進行管理,下圖顯示了受支持的播放控制操作驅動的MediaPlayer物件的生命周期和狀態, 橢圓表示MediaPlayer物件可能駐留的狀態,弧表示驅動物件狀態轉換的播放控制操作,有兩種型別的弧, 單箭頭的弧表示同步方法呼叫,而雙箭頭的弧表示異步方法呼叫,
從這個狀態圖中,可以看到MediaPlayer物件具有以下狀態:
- 當使用 new 創建MediaPlayer物件或者在呼叫 reset() 之后,它處于空閑狀態; 并且在呼叫 release() 之后,它處于 End 狀態, 在這兩個狀態之間是MediaPlayer物件的生命周期,
- 通常,一些播放控制操作可能由于各種原因而失敗,例如不支持的音頻/視頻格式,交錯的音頻/視頻,解析度太高,流超時等,因此,在這些情況下,關注錯誤報告和恢復是非常重要的,有時,由于編程錯誤,也可能在無效狀態下呼叫播放控制操作,在所有這些錯誤條件下,如果開發者事先通過setOnErrorListener(android.media.MediaPlayer.OnErrorListener)注冊了 one rrorListener ,則內部播放器引擎會呼叫開發者提供的 one rrorListener.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. |
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/212109.html
標籤:其他
