android mediaplayer prepare

來源:互聯網
上載者:User

         android 的mediaplayer的prepare過程有很多動作,比方demuxer,find codec,如果時間較長就會出現ANR,android的系統會在5秒檢測主線程是否有響應,沒有就會出現錯誤提示,使用者體驗非常的不好。所以系統如果有parepareAsync提供的話,可以使用這個函數,然後再檢測prepare狀態,如果沒有prepareAsync提供的話,prepare不能在主線程來使用。         下面是從SDK摘出來的


Asynchronous Preparation

Using MediaPlayer can be straightforward in principle. However,
it's important to keep in mind that a few more things are necessary to integrate it correctly with a typical Android application. For example, the call to prepare() can
take a long time to execute, because it might involve fetching and decoding media data. So, as is the case with any method that may take long to execute, you should never call it from your application's UI thread. Doing that will cause the
UI to hang until the method returns, which is a very bad user experience and can cause an ANR (Application Not Responding) error. Even if you expect your resource to load quickly, remember that anything that takes more than a tenth of a second to respond in
the UI will cause a noticeable pause and will give the user the impression that your application is slow.

To avoid hanging your UI thread, spawn another thread to prepare the MediaPlayer and
notify the main thread when done. However, while you could write the threading logic yourself, this pattern is so common when using MediaPlayer that
the framework supplies a convenient way to accomplish this task by using the prepareAsync() method.
This method starts preparing the media in the background and returns immediately. When the media is done preparing, the onPrepared() method
of the MediaPlayer.OnPreparedListener, configured
throughsetOnPreparedListener() is
called

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.