Java 我的mediaplayer功能有什么问题?
我花了十多个小时在这上面,尝试了许多在互联网上找到的解决方案,感觉自己很愚蠢。。。 此函数仅在MediaPlayer中加载ogg文件。它以字符串参数的形式获取文件的url。它首先提取文件名,并尝试从Java 我的mediaplayer功能有什么问题?,java,android,android-mediaplayer,Java,Android,Android Mediaplayer,我花了十多个小时在这上面,尝试了许多在互联网上找到的解决方案,感觉自己很愚蠢。。。 此函数仅在MediaPlayer中加载ogg文件。它以字符串参数的形式获取文件的url。它首先提取文件名,并尝试从res/raw/加载文件,如果文件存在,则从网站获取 MediaPlayer player ; void snd_load(String url) { String id_snd = url.substring(url.lastIndexOf("/") + 1); // name of t
res/raw/
加载文件,如果文件存在,则从网站获取
MediaPlayer player ;
void snd_load(String url) {
String id_snd = url.substring(url.lastIndexOf("/") + 1); // name of the file
try{
player.setDataSource( "android.resource://didi.a8bitpocketwrestlers/res/raw/" + id_snd );
}
catch (Exception e){
Toast.makeText(getApplicationContext(), "can't open file, will try to download it"+"\n"+e , Toast.LENGTH_SHORT).show();
try{ player.setDataSource(url); }
catch (Exception e2){ Toast.makeText(getApplicationContext(), "can't download file"+"\n"+e2 , Toast.LENGTH_SHORT).show(); }
}
}
我得到了java.lang.NULLPointerException
错误。
我现在明白了,播放器只是MediaPlayer类型的,它还没有指向任何东西。我错了,因为里面有属性,所以我认为对象存在。我必须做player=newmediaplayer()
或player=MediaPlayer.create()代码>
现在我解决了这个问题,仍然无法加载文件
04-22 15:31:58.018 130-4150/?E/FileSource:无法打开文件
“安卓。resource://didi.a8bitpocketwrestlers/snd_title.ogg'. (没有
文件或目录)04-22 15:31:58.018
4087-4099/didi.A8比特袋摔跤手E/MediaPlayer:错误(1,
-2147483648)04-22 15:31:58.018 4087-4087/didi.A8BitPocket摔跤手E/MediaPlayer:错误(1,-2147483648)
它停止,不显示消息,也不尝试下载消息
使用player=MediaPlayer.create(getApplicationContext(),R.raw.snd_title)代码>它崩溃是因为我的player.prepareAsync()代码>呼叫。我读到了。create已经处理好了,所以我不能调用。prepareAsync();也不使用.setOnPreparedListener()。这对我来说是个问题,我需要知道它什么时候准备好
无论如何,我必须在变量id_snd中使用名称,而不仅仅是snd_title.ogg文件,所以我将其更改为player=MediaPlayer.create(getApplicationContext(),getApplicationContext().getResources().getIdentifier(id_snd,“raw”,getApplicationContext().getPackageName())代码>我得到
android.content.res.Resources$NotFoundException:资源ID#0x0
新版本
MediaPlayer player ;
void snd_load(String url) {
String id_snd = url.substring(url.lastIndexOf("/") + 1); // name of the file
id_snd = id_snd.substring( 0 , id_snd.indexOf(".") ) ; // to remove ".ogg" or ".aac"
try{
player = MediaPlayer.create(getApplicationContext(), getApplicationContext().getResources().getIdentifier(id_snd, "raw", getApplicationContext().getPackageName()) );
}
catch (Exception e){
Toast.makeText(getApplicationContext(), "can't open file, will try to download it"+"\n"+e , Toast.LENGTH_SHORT).show();
try{ player = MediaPlayer.create(getApplicationContext(), Uri.parse(url)); }
catch (Exception e2){ Toast.makeText(getApplicationContext(), "can't download file"+"\n"+e2 , Toast.LENGTH_SHORT).show(); }
}
}
使用以下代码获取该文件的resourceId
int resId = getResources().getIdentifier(audioName, "raw", getPackageName());
audioName应该没有任何扩展名
并使用以下代码创建您的播放器对象
player = MediaPlayer.create(context,resId);
你有WRITE\u EXTERNAL\u STORAGE
权限吗?我编辑过,因为空指针只是问题的一部分。@Reaz\u Murshed:没有,我更喜欢避免添加读/写外部存储的权限,因为人们越来越偏执,认为应用程序会“窥探”他们的sd卡。有什么理由调用getIdentifier()
如果你有R.raw.snd_title
?@pskink不仅仅是我有这个练习,这就是为什么我在那里添加了它…什么练习?你想得到剩余的
,对吗?所以使用R.raw.snd_title
-这是你的resId
@pskink是的,你是对的。。。我们可以用这个。。我是否应该编辑我的答案并直接编写R.raw.snd_title
?snd_title.ogg只是其中一个文件,我编写它是为了在id_snd变量出现问题时进行尝试。谢谢你告诉我如何从文件名中获取id,这正是我所需要的。但是,我需要添加getApplicationContext()。在getResources()和getPackageName()之前。