Android 原生 MediaPlayer 和 MediaCodec 的區別和聯繫(二)

来源:https://www.cnblogs.com/nmj1986/archive/2018/07/13/9307588.html
-Advertisement-
Play Games

目錄: (3)Android 官方網站 對 MediaPlayer的介紹 正文: Android 官方網站 對 MediaPlayer的介紹 MediaPlayer public class MediaPlayer extends Object implements VolumeAutomation ...


目錄:

  (3)Android 官方網站 對 MediaPlayer的介紹

正文:

    Android 官方網站 對 MediaPlayer的介紹        

        MediaPlayer       public class MediaPlayer        extends Object implements VolumeAutomationAudioRouting           MediaPlayer類被用來控制音/視頻文件和流的播放。可以在VideoView中找到有關如何使用此類中的方法的示例。         這裡涉及的主題是:         1. 狀態圖         2. 有效和無效狀態         3. 許可權         4. 註冊信息和錯誤回調                  ★ 開發者指南              有關如何使用MediaPlayer的更多信息,請閱讀Media Playback開發人員指南。           狀態圖         音/視頻文件和流的播放控制是作為一個狀態機來進行管理。下圖顯示了受支持的播放控制操作驅動的MediaPlayer對象的生命周期和狀態。 橢圓表示MediaPlayer對象可能駐留的狀態。弧表示驅動對象狀態轉換的播放控制操作。有兩種類型的弧。 單箭頭的弧表示同步方法調用,而雙箭頭的弧表示非同步方法調用。                            從這個狀態圖中,可以看到MediaPlayer對象具有以下狀態:
  • 當使用 new 創建MediaPlayer對象或者在調用 reset() 之後,它處於空閑狀態; 並且在調用 release() 之後,它處於 End 狀態。 在這兩個狀態之間是MediaPlayer對象的生命周期。
            1. 一個新構造的 MediaPlayer 對象 和 調用 reset() 方法後的MediaPlayer對象之間存在微妙但重要的區別。針對這兩種情況的空閑狀態下調用諸如 getCurrentPosition()getDuration()getVideoHeight(),getVideoWidth(),setAudioAttributes(AudioAttributes)setLooping(boolean),setVolume(float, float),pause()start()stop()seekTo(long, int)prepare() or prepareAsync() 方法是程式設計錯誤。如果在一個 MediaPlayer 對象被構造後任意調用這些方法,則內部播放引擎不會調用用戶提供的回調方法OnErrorListener.onError(),並且該對象狀態保持不變;但是如果這些方法是在reset()後被調用,則內部播放引擎將調用用戶提供的回調方法OnErrorListener.onError(),並且該對象將被轉換為 Error 狀態。             2. 還建議一旦不再使用MediaPlayer對象,立即調用release(),以便可以立即釋放與MediaPlayer對象關聯的內部播放器引擎使用的資源。 資源可能包括單一資源(如硬體加速組件)和調用release()失敗可能導致MediaPlayer對象的後續實例回退到軟體實現或完全失敗(?)。 一旦MediaPlayer對象處於End狀態,就無法再使用它,也無法將其恢復到任何其他狀態。             3. 此外,使用new創建的MediaPlayer對象處於空閑狀態,而使用其中一個重載的方便的創建方法創建的對象不處於空閑狀態。 實際上,如果使用create方法創建成功,則對象處於Prepared狀態。
  • 通常,一些播放控制操作可能由於各種原因而失敗,例如不支持的音頻/視頻格式,交錯的音頻/視頻,解析度太高,流超時等。因此,在這些情況下,關註錯誤報告和恢復是非常重要的。有時,由於編程錯誤,也可能在無效狀態下調用播放控制操作。在所有這些錯誤條件下,如果開發者事先通過setOnErrorListener(android.media.MediaPlayer.OnErrorListener)註冊了 OnErrorListener ,則內部播放器引擎會調用開發者提供的 OnErrorListener.onError() 方法。
            1. 重要的是要註意,一旦發生錯誤,MediaPlayer對象就會進入錯誤狀態(Error state)(除非如上所述),即使應用程式尚未註冊錯誤監聽器也是如此。             2. 為了重用處於錯誤狀態的MediaPlayer對象並從錯誤中恢復,可以調用reset()將對象恢復到其空閑狀態(Idle state)。             3. 讓應用程式註冊OnErrorListener以查找內部播放器引擎的錯誤通知是一種很好的編程習慣。             4. 調用譬如 prepare(),prepareAsync()時,或者一個在無效狀態(Idle state)重寫的 setDataSource  方法時,拋出IllegalStateException 可以防止編程錯誤。            將一個 MediaPlayer 對象從空閑狀態(Idle state) 轉換為 初始狀態(Initialized state)。             1. 如果在任何其他狀態下調用 setDataSource() ,則拋出 IllegalStateException。             2. 關註從重載的 setDataSource 方法 可能會拋出 IllegalArgumentException 和 IOException 是一種很好的編程習慣。
  • 在開始播放之前,MediaPlayer 對象必須先進入準備狀態。
            1. 有兩種方法(同步與非同步)可以達到Prepared狀態(Prepared state):調用prepare()(同步),一旦方法調用返回就將對象轉換為Prepared狀態(Prepared state),或者調用prepareAsync()( 非同步),它在調用返回後首先將對象轉換為Preparation狀態(Preparing state)(幾乎正確地發生),同時內部播放器引擎繼續處理其餘的準備工作,直到準備工作完成。 當準備完成或者prepare() 調用返回時,如果事先通過setOnPreparedListener(android.media.MediaPlayer.OnPreparedListener)註冊了OnPreparedListener,則內部播放器引擎會調用開發者提供的OnPreparedListener介面的回調方法onPrepared()。             2. 重要的是要註意,準備狀態是暫時狀態,並且在MediaPlayer對象處於準備狀態時調用任何具有副作用的方法的行為是未定義的。             3. 在任何其他狀態調用 prepare() 或 prepareAsync() ,則拋出 IllegalStateException。             4. 在Prepared狀態(Prepared state)下,可以通過調用相應的set方法來調整音頻/音量,screenOnWhilePlaying,迴圈等屬性。
  • 要開始播放,必須調用 start() ,start() 返回成功後,MediaPlayer對象則處於 Started狀態(Started state)。isPlaying()可用來測試  MediaPlayer對象是否處於 Started狀態(Started state)。
            1. 處於Started狀態(Started state)時,如果事先通過 setOnBufferingUpdateListener(OnBufferingUpdateListener)註冊了OnBufferingUpdateListener,則內部播放器引擎會調用用戶提供的.OnBufferingUpdateListener.onBufferingUpdate() 回調方法。 此回調允許應用程式在流式傳輸音頻/視頻時跟蹤緩衝狀態。             2. 調用 start() 對已處於Started狀態的MediaPlayer對象沒有影響。
  • 播放可以暫停和停止,並可以調整當前播放位置。 可以通過pause()暫停播放。 當對pause()的調用返回時,MediaPlayer對象進入Paused狀態(Pausedstate)。 請註意,從“已啟動”狀態(Started state)到“暫停”狀態(Paused state)的轉換(反之亦然)在播放器引擎中非同步發生。 在調用isPlaying()時更新狀態可能需要一些時間,對於流內容,它可能需要幾秒鐘。
            1. 調用start()以恢復暫停的MediaPlayer對象的播放,並且恢復的播放位置與暫停的位置相同。 當對start()的調用返回時,暫停的MediaPlayer對象將返回到Started狀態(Started state)。             2. 調用pause()對已處於Paused狀態的MediaPlayer對象沒有影響。
  • 調用stop()會停止播放並導致處於StartedPausedPreparedPlaybackCompleted狀態(state)的MediaPlayer進入Stopped狀態(Stopped state)。
            1. 一旦處於Stopped狀態(Stopped state),在調用prepare()prepareAsync()以將MediaPlayer對象再次設置為Prepared狀態(Prepared state)之前,無法啟動播放。             2. 調用stop()對已處於Stopped狀態(Stopped state)的MediaPlayer對象沒有影響。             1. 儘管非同步seekTo(long, int)調用立即返回,但實際的尋位操作可能需要一段時間才能完成,特別是對於流式傳輸的音頻/視頻。 當實際尋位操作完成時,如果事先通過setOnSeekCompleteListener(OnSeekCompleteListener)註冊了OnSeekCompleteListener,則內部播放器引擎會調用開發者提供的OnSeekComplete.onSeekComplete() 。             2. 請註意,seekTo(long, int)也可以在其他狀態中調用,例如PreparedPausedPlaybackCompleted狀態(state)。 當在這些狀態中調用seekTo(long, int)時,如果流具有視頻且請求的位置有效,則將顯示一個視頻幀。             3. 此外,可以通過調用getCurrentPosition()來檢索實際當前播放位置,這對於需要跟蹤播放進度的音樂播放器等應用程式很有幫助。
  • 當播放到達流的結尾時,播放完成。
            1. 如果使用setLooping(boolean)將迴圈模式設置為true,則MediaPlayer對象應保持為Started狀態(Started state)。             2. 如果迴圈模式設置為false,則播放器引擎調用開發者提供的回調方法OnCompletion.onCompletion(),如果事先通過 setOnCompletionListener(OnCompletionListener)註冊了OnCompletionListener。 調用回調信號表示對象現在處於PlaybackCompleted狀態(PlaybackCompleted state)。             3. 在PlaybackCompleted狀態(PlaybackCompleted state)下,調用start()可以從音頻/視頻源的開頭重新開始播放。           有效和無效狀態     
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.
          許可權         可能需要聲明相應的WAKE_LOCK許可權<uses-permission>元素。         當使用網路內容時該類要求聲明 Manifest.permission.INTERNET 許可權。           回調         應用程式可能希望註冊信息和錯誤事件,以便在播放或流式傳輸期間獲知某些內部狀態更新和可能的運行時錯誤。註冊這些事件是由正確設置相應的監聽器(通過調用setOnPreparedListener(OnPreparedListener) setOnPreparedListener,setOnVideoSizeChangedListener(OnVideoSizeChangedListener) setOnVideoSizeChangedListener,setOnSeekCompleteListener(OnSeekCompleteListener)setOnSeekCompleteListener,setOnCompletionListener(OnCompletionListener) setOnCompletionListener,setOnBufferingUpdateListener(OnBufferingUpdateListener) setOnBufferingUpdateListener,setOnInfoListener(OnInfoListener) setOnInfoListener,setOnErrorListener(OnErrorListener) setOnErrorListener等完成)。 為了接收與這些偵聽器關聯的相應回調,應用程式需要在運行自己的Looper線程上創建MediaPlayer對象(預設情況下,主UI線程正在運行Looper)。
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 性能更好的新伺服器申請下來了,我們決定在2台新伺服器上使用mysql5.7,並且使用主從同步、讀寫分離架構,很不幸這個任務落到了我的頭上。讀寫分離是在業務代碼中實現的,在此不做詳述,介紹一下我搭建MySQL主從的過程。 環境介紹: Master 10.20.66.150 Slave 10.20.66 ...
  • 1、cd到mysql安裝目錄bin目錄: 2、輸入id、用戶名和密碼: 3、查看資料庫實例: 4、創建一個實例: 5、刪除一個實例: 6、創建一個表: 7、刪除一個表: 8、表結構: 9、修改表: 你想在一個庫裡面建表的時候 首先你要記得use 使用當前的庫 use庫名 創建表: create ta ...
  • 簡介 在資料庫中,我們除了存儲數據外,還存儲了大量的元數據。它們主要的作用就是描述資料庫怎麼建立、配置、以及各種對象的屬性等。本篇簡單介紹如何使用和查詢元數據,如何更有效的管理SQLServer 資料庫。 對一些有經驗的資料庫開發和管理人員而言,元數據是非常有價值的。下麵我會介紹一下簡單的原理,然後 ...
  • 占座 ...
  • 這種情況一般是你以前安裝過MySQL資料庫服務項被占用了。解決方法: 方法一:安裝MySQL的時候在這一步時它預設的服務名是“MySQL” 只需要把這個名字改了就可以了。可以把預設的伺服器的名稱手動改為你沒用過的其他名稱。 方法二:1、卸載MySQL 2、刪除安裝目錄及數據存放目錄 3、在註冊表(r ...
  • 1.瞭解大數據理論要學習大數據你至少應該知道什麼是大數據,大數據一般運用在什麼領域。對大數據有一個大概的瞭解,你才能清楚自己對大數據究竟是否有興趣,如果對大數據一無所知就開始學習,有可能學著學著發現自己其實不喜歡,這樣浪費了時間精力,可能還浪費了金錢。所以如果想要學習大數據,需要先對大數據有一個大概 ...
  • 背景: 朋友的環境第二天突然訪問不了SQL Server,遠程SQL Server用戶無法登陸,但是本地SQL Server用戶登錄正常。 報錯: 用戶XX登錄失敗(MicroSoft SQL Server,錯誤18456) 排查: 對與無法連接伺服器的,一般的排查手段,也是最常用的手段。 1.因為 ...
  • 一、前言 背景:一些公立單位伺服器是不允許使用window7以上的系統的,而大部分都是window server系列的系統 首先大部分軟體安裝預設是在C盤,而一部分人安裝軟體是不喜歡預設安裝在C盤的,window7以上的系統安裝MySql都可以修改Data文件的存儲路徑。 但是我在window se ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...