android 的mediaplayer的prepare过程有很多动作,比方demuxer,find codec,如果时间较长就会出现ANR,android的系统会在5秒检测主线程是否有响应,没有就会出现错误提示,用户体验非常的不好。所以系统如果有parepareAsync提供的话,可以使用这个函数,然后再检测prepare状态,如果没有prepareAsync提供的话,prepare不能在主线程来使用。
下面是从SDK摘出来的
Asynchronous Preparation
UsingMediaPlayer
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 toprepare()
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 shouldnever 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 theMediaPlayer
and
notify the main thread when done. However, while you could write the threading logic yourself, this pattern is so common when usingMediaPlayer
that
the framework supplies a convenient way to accomplish this task by using theprepareAsync()
method.
This method starts preparing the media in the background and returns immediately. When the media is done preparing, theonPrepared()
method
of theMediaPlayer.OnPreparedListener
, configured
throughsetOnPreparedListener()
is
called
分享到:
相关推荐
Android 视频播放demo MediaPlayer+Surface实现 播放网络视频: MyMediaPlayer.getInstance().play("http://download.cntv.cn/docuchina/PSA/2017/08/28/3b69f457848e0b497b12e356376515df_mpeg28000000PCM.mp4"); ...
Android 程序技术 本节课程内容:多媒体 PART1 多媒体 MediaPlayer播放音频 VideoView播放视频 MediaPlayer播放音频 MediaPlayer plays audio MediaPlayer是用于播放音频和视频的,它支持多种格式音频文件(3gp、...
将音乐以Import的方式存储在res/raw/always.mp3,在Android系统运行播放音乐的按钮,覆盖OnClick事件,在MediaPlayer取得播放资源与stop()之后,要准备Playback的状态前一定要使用MediaPlayer.prepare(),...
import android.media.MediaPlayer; public class SoundService { Map, MediaPlayer> sounds = new HashMap, MediaPlayer>(); Context context = null; public SoundService(Context context) { this....
我们都知道需要播放一个视频需要三个过程 1.create MediaPlayer 2.MediaPlayer prepare 3.MediaPlayer 与 Surface 绑定 xinvideoplayer中使用了VideoManager来控制ijkmediaplayer 所有的操作 三个过程 我们使用 ...
使用MediaPlayer 制作一个 视频播放器 ,可指定视频的大小、预览的模式之外,还提供对 prepare,OnCompletionListener 等事件的判断。 通过创建SurfaceView 视图与控制SurfaceView 的 SurfaceHolder 对象,搭配 ...
mediaPlayer.prepare(); } catch (IOException e) { mediaPlayer = null; } } } private static final long VIBRATE_DURATION = 200L; private void playBeepSoundAndVibrate() { if ...
嵌入式Android项目设计与开发 第十章 多媒体 —— 音频的应用 音频应用 Android上的音频主要使用MediaPlayer控件,该控件有多个常用方法: setDataSource:设置播放资源 prepare:播放就绪 start 、stop、pause:...
在 Android 中播放音频文件一般都是使用 MediaPlayer 类来实现的,它对多种格式的音 频文件提供了非常全面的控制方法,从而使得播放音乐的工作变得十分简单。下表列出了 MediaPlayer 类中一些较为常用的控制方法。 ...
player = MediaPlayer.create(this, R.raw.xiaoxiaole); try { player.prepare();// 同步 } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch ...