Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的mediaplayer功能有什么问题?_Java_Android_Android Mediaplayer - Fatal编程技术网

Java 我的mediaplayer功能有什么问题?

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

我花了十多个小时在这上面,尝试了许多在互联网上找到的解决方案,感觉自己很愚蠢。。。 此函数仅在MediaPlayer中加载ogg文件。它以字符串参数的形式获取文件的url。它首先提取文件名,并尝试从
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()之前。