MediaPlayer.start()是否也应该是新线程? [英] Should MediaPlayer.start() also be a new thread?

查看:203
本文介绍了MediaPlayer.start()是否也应该是新线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处的教程说明服务实际上使用了主线程.因此,它使用prepareAsync来避免阻塞UIS: http://developer.android.com/guide/topics/media/mediaplayer.html#asyncprepare

The tutorial here explains that a service actually uses the main thread. So it uses prepareAsync to avoid blocking UIS: http://developer.android.com/guide/topics/media/mediaplayer.html#asyncprepare

我想知道异步回调onPrepared在哪里运行.在示例中,onPrepared调用MediaPlayer的开始.启动也是一种占用大量CPU的方法吗?如果它在同一线程中运行,它也会阻塞.

I was wondering where the async callback onPrepared runs. In the example onPrepared calls start of MediaPlayer. Is start also a CPU-intensive method? If it runs in the same thread, it also blocks.

推荐答案

MediaPlayer.start()至少不是一项密集的操作. MediaPlayer使用其自己的本机线程来执行任务,但是对UI线程而言,对同步prepare方法的调用可能会花费很长时间,尤其是当它是您要播放的远程媒体时.在这种情况下,它必须等待一个或多个网络请求,要缓冲的数据等.如果onPrepared回调将在主线程上发生,如果那是您调用prepareAsync的地方(或您从中调用它的任何线程),更精确).

MediaPlayer.start() is not an intensive operation in the least. The MediaPlayer uses its own native thread to perform tasks, but a call to the synchronous prepare method can take too long for the UI thread, particularly if it is remote media you are trying to play. In that case it has to wait for one or more network requests, data to buffer, etc. The onPrepared callback will happen on the main thread if that is where you called prepareAsync (or whatever thread you called it from, to be more precise).

这篇关于MediaPlayer.start()是否也应该是新线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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