Android:PrepareAsync()和Prepare()之间的媒体播放器区别 [英] Android:Media Player difference between PrepareAsync() and Prepare()

查看:370
本文介绍了Android:PrepareAsync()和Prepare()之间的媒体播放器区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现基本的媒体播放器功能,并且在PrepareAsync()之间感到困惑 和Prepare()方法调用.如果音频文件位于原始文件夹中,则应使用哪一个.

解决方案

这些方法之间的区别主要在于它们在哪个线程上执行.

Prepare在您称为它的线程(最常见的UI线程)中运行,因此,如果花费很长时间(缓冲来自Internet等的视频),它将阻塞您的UI线程,并且用户可能会获得ANR.

PrepareAsync在后台线程中运行,因此UI线程几乎立即返回,因此不会被阻塞.但是播放器尚未准备好,因此您需要设置onPreparedListener以便知道何时可以使用MediaPlayer.

I wanted to implement basic media player functionality and was confused between PrepareAsync() and Prepare() method calls. Which one should be used if the audio file is in the raw folder .

解决方案

The difference between those methods is basically in what thread they're executed.

Prepare runs in the thread you call it (most frequently UI thread) and thus if it takes long time (buffering video from the Internet and such) it will block your UI thread and a user might get ANR.

PrepareAsync runs in a background thread and thus UI thread is not blocked as it returns almost immediately. But the player is not prepared so you want to set onPreparedListener in order to know when the MediaPlayer is ready to be used.

这篇关于Android:PrepareAsync()和Prepare()之间的媒体播放器区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆