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
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...