错误创建资产的MediaPlayer与URI或文件 [英] Error creating MediaPlayer with Uri or file in assets

查看:148
本文介绍了错误创建资产的MediaPlayer与URI或文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我复制Song.mp3的到我的项目的资产目录,并写了code:

I copied song.mp3 to my project's assets directory and wrote this code:

private MediaPlayer mp;

Uri uri = Uri.parse("file:///android_asset/song.mp3");

mp=MediaPlayer.create(this, uri);

运行create语句后,变量熔点为空。什么是错的?

After running the create statement, the variable mp is null. What is wrong?

感谢。

推荐答案

试试这个,看看是否有异常被捕获:

Try this and see if any exceptions are caught:

try {
    MediaPlayer mp = new MediaPlayer();
    mp.setDataSource(this, uri);
}
catch (NullReferenceArgument e) {
    Log.d(TAG, "NullReferenceException: " + e.getMessage());
}
catch (IllegalStateException e) {
    Log.d(TAG, "IllegalStateException: " + e.getMessage());
}
catch (IOException e) {
    Log.d(TAG, "IOException: " + e.getMessage());
}
catch (IllegalArgumentException e) {
    Log.d(TAG, "IllegalArgumentException: " + e.getMessage());
}
catch (SecurityException e) {
    Log.d(TAG, "SecurityException: " + e.getMessage());
}

者将解释该异常是怎么回事错在你的创作。据在该文档中,静态create方法就是简写是什么在try块以上。我可以看到的主要区别是,静态方法来创建,而确实的setDataSource不会抛出。

The exception caught will explain what is going wrong in your create. According the the docs, the static create method is just shorthand for what is in the try block above. The major difference that I can see is that the static method create doesn't throw while setDataSource does.

这篇关于错误创建资产的MediaPlayer与URI或文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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