的MediaPlayer的setDataSource,最好使用路径或数据的FileDescriptor? [英] MediaPlayer setDataSource, better to use path or FileDescriptor?

查看:846
本文介绍了的MediaPlayer的setDataSource,最好使用路径或数据的FileDescriptor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有一个完整的文件路径。哪个是更好的方法来加载该文件导入到MediaPlayer的?

Let's say I have a full path to a file. Which is the better approach to loading that file into a MediaPlayer?

String filePath = "somepath/somefile.mp3";
mediaPlayer.setDataSource(filePath);

String filePath = "somepath/somefile.mp3";
File file = new File(filePath);
FileInputStream inputStream = new FileInputStream(file);
mediaPlayer.setDataSource(inputStream.getFD());
inputStream.close();

什么关系呢?只需用路径似乎更容易,但有一个理由做额外的工作来使用的FileDescriptor?

Does it matter? Simply using the path seems easier but is there a reason to do the extra work to use a FileDescriptor?

推荐答案

其实,事实证明,有在某些情况下的差异。

Actually, it turns out that there IS a difference in certain situations.

mediaPlayer.setDataSource(字符串路径)当你调用失败媒体播放器。prepare(),如果你要加载从 getApplicationContext()的文件。getFilesDir(),根据文件是如何保存的。例如,如果我写使用文件新RandomAccessFile的(文件路径,RW),该文件其实不是,如果你使用的媒体播放器可读取 mediaPlayer.setDataSource(字符串路径)。该 prepare()将立即触发错误(1,-2147483648)从媒体播放器;本质上是文件权限错误。 SDK 9介绍 file.setReadable(布尔可读,布尔ownerOnly)这将presumably允许您通过设置来解决这个问题 ownerOnly 来假的... ...但是这并不能帮助你,如果你需要支持旧的软件开发工具包。

mediaPlayer.setDataSource(String path) will fail when you call mediaPlayer.prepare(), if you are trying to load a file from getApplicationContext().getFilesDir(), depending on how the file was saved. For example, if I write a file using new RandomAccessFile(filePath, "rw"), the file is not actually readable by the mediaplayer if you use mediaPlayer.setDataSource(String path). The prepare() will immediately trigger error(1, -2147483648) from the mediaplayer; essentially a file permission error. SDK 9 introduced file.setReadable (boolean readable, boolean ownerOnly) which would presumably allow you to resolve this issue by setting ownerOnly to false...but that doesn't help you if you need to support older SDKs.

不过, mediaPlayer.setDataSource(的FileDescriptor FD)没有这个问题,媒体播放器将成功prepare没有权限问题完全相同的文件。

HOWEVER, mediaPlayer.setDataSource(FileDescriptor fd) does NOT have this problem and mediaplayer will successfully prepare the same exact file without a permission problem.

这篇关于的MediaPlayer的setDataSource,最好使用路径或数据的FileDescriptor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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