Media Player设置播放资源时要注意的问题

用MediaPlayer加载播放资源的时候一直遇到(-38,0), 仔细看了终于发现了问题所在,因为要加载/data/data/<packageName>/下面的文件,我用了MediaPlayer.setDataSource(String path)这个方法,原来API里已经明确提到,这个文件有可能会被其他process而不是调用的application使用,所以必须要是全局可访问的外部文件,放在/data/data/<packageName>/下面,就不行了。官方说明如下

When path refers to a local file, the file may actually be opened by a process other than the calling application. This implies that the pathname should be an absolute path (as any other process runs with unspecified current working directory), and that the pathname should reference a world-readable file. As an alternative, the application could first open the file for reading, and then use the file descriptor form setDataSource(FileDescriptor).

所以更改为其他方法,

FileInputStream fileInputStream = new FileInputStream(filePath);
mMediaPlayer.setDataSource(fileInputStream.getFD());

这样就可以了。

另外,如果加载文件为asset下面的,可以用如下代码设置:

AssetFileDescriptor afd = mParentActivity.getAssets() .openFd(filename);
mMediaPlayer.setDataSource(afd.getFileDescriptor(),
afd.getStartOffset(), afd.getLength());
afd.close();


转载于:https://my.oschina.net/u/268088/blog/369458

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值