当前位置:操作系统 > 安卓/Android >>

android mediaplayer prepare

 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


摘自 shcalm的专栏
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,