的MediaPlayer的setDataSource需要最佳实践的建议 [英] MediaPlayer setDataSource need best practice advice

查看:1043
本文介绍了的MediaPlayer的setDataSource需要最佳实践的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读媒体播放和后的 MediaPlayer的Android的文档,有些我有一些困惑和需要有关<一个经验丰富的咨询href="http://developer.android.com/reference/android/media/MediaPlayer.html#setDataSource%28android.content.Context,%20android.net.Uri%29"相对=nofollow>的setDataSource 重载方法。 我使用的MediaPlayer 服务分量在我的项目,该项目将是一个的 foregroundService 同时播放音乐。我在 RES /生我的apk文件夹我的音乐文件(MP3播放)。 开始玩,我知道我必须prepare的MediaPlayer对象。由于服务在Android应用程序在默认情况下使用单进程和主线程,我不希望我的用户获得的 ANR 而MediaPlayer的prepares本身(认为如果原文件夹中的媒体文件有一个大尺寸)。 然后使用 prepareAsync 而不是 prepare (同​​步)。所以我不能使用:

After reading "Media Playback" and "MediaPlayer" android documentations, some i have some confusions and need experienced advice about setDataSource overloaded method. I am using MediaPlayer in a Service component in my Project that is going to be a foregroundService while playing music. I have my music file(.mp3) in res/raw folder of my apk. To start playing, i know i have to prepare the MediaPlayer object. Because Services in android applications by default uses single process and main thread, i dont want my users get ANR while MediaPlayer prepares itself(think if media file in raw folder has a big size). Then i use prepareAsync instead of prepare(Sync). So i can not use:

mp = MediaPlayer.create(context, R.raw.myfile);

由于这已经调用 prepare()内部而不是 prepareAsync()。 所以基本上我有两个选择(二四个):

Because this already calls prepare() internally but not prepareAsync(). So basically i have two options(two from four):

Uri myUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.myfile);
mp.setDataSource(context, myUri);

AssetFileDescriptor afd = context.getResources().openRawResourceFd(R.raw.myfile);
mp.setDataSource(fd.getFileDescriptor());
afd.close();

使用其中的一个后,我可以简单的使用:

after using one of them i can simple use:

mp.prepareAsync();

最后我的问题出现的包括这些不同的方法,哪一个是最好的选择?是否有什么好处一个比其他?我失去了一些东西?

And finally my questions arise that "including these different methods, which one is the best option? Are there any benefits one over the other? Do i missing something?"

推荐答案

有没有调用的各种方式中的任何实际的好处创建的setDataSource 。静态创建的方法不会做的比电话更的setDataSource prepare 。各种的setDataSource 方法相互调用内部。最终,他们归结为两种可能的本地通话,其中一个描述与本地文件描述符远程URI和另一个字符串。有可能是一个非常轻微的性能优势,创建自己的文件描述符,但它不会是AP preciable融会贯通。

There aren't any real benefits to the various ways of calling create or setDataSource. The static create methods don't do much more than call setDataSource and prepare. The various setDataSource methods call each other internally. Eventually they boil down to two possible native calls, one with a string describing a remote URI and another with a local file descriptor. There may be a very slight performance advantage to creating the file descriptor yourself, but it will not be appreciable in context.

有关本地文件播放,为你展示你的code,只需调用 prepare (或静态创建方法)是个不错的做法的。底层玩家应该没有问题确定相关的元数据和返回的快速不管该文件的大小。该 prepareAsync 方法是网络流,其中任何数量的情况下,可能会造成一些意想不到的延误更加有用。如果你正在设计一个通用的球员,然后用 prepareAsync 方法是将要走的路,但如果你只是玩原始资产不应该做任何区别。在各种提供的方法仅仅是为了方便起见(注意Javadoc中的创建)。

For local file playback, as you are demonstrating in your code, simply calling prepare (or the static create methods) isn't a bad practice at all. The underlying player should have no problem determining the relevant metadata and returning quickly no matter the size of the file. The prepareAsync method is more useful for network streams, where any number of situations might cause some unexpected delay. If you are designing a general purpose player, then using the prepareAsync method would be the way to go, but if you are simply playing raw assets it shouldn't make any difference. The variety of methods provided is simply a matter of convenience (take note of the javadoc for create).

这篇关于的MediaPlayer的setDataSource需要最佳实践的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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