Java 媒体播放器在 R.raw 上失败 - Android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18134346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:38:22  来源:igfitidea点击:

mediaplayer failing on R.raw - Android

javaandroidandroid-mediaplayer

提问by epsilones

Importing import android.media.MediaPlayer, I am told raw cannot be resolvedin

导入import android.media.MediaPlayer,有人告诉我raw cannot be resolved

 private void playSound(){
        MediaPlayer mp = MediaPlayer.create(this, R.raw.Jam);
...

I am really new to Android, what is rawfor ? and how can I fix this problem ?

我真的很新Android,是raw为了什么?我该如何解决这个问题?

采纳答案by William Morrison

A raw folder holds files of any type. You need a rawfolder under your resources folder (res). In your example, jamis expected as a resource in the rawfolder, and will likely be a type of music file as you're using MediaPlayerto try and read it.

原始文件夹包含任何类型的文件。您raw的资源文件夹 ( res)下需要一个文件夹。在您的示例中,jam应作为raw文件夹中的资源,并且可能是您MediaPlayer用来尝试阅读的一种音乐文件。

You've this error raw cannot be resolvedbecause raw folder doesn't exist, and so the variable rawin class Ris not being auto-generated.

您遇到此错误raw cannot be resolved是因为原始文件夹不存在,因此raw类中的变量R不是自动生成的。

Fix the problem by creating the rawfolder.

通过创建raw文件夹来解决问题。

If you already have created res/rawtry cleaning the project. Sometimes Eclipse gets confused. If that doesn't work, make a small change to a source file, and save it so the auto-build process kicks off. Sometimes cleaning manually hasn't fixed the problem for me, its a known bug for Eclipse.

如果您已经创建,请res/raw尝试清理项目。有时 Eclipse 会感到困惑。如果这不起作用,请对源文件进行小的更改,然后保存它,以便自动构建过程开始。有时手动清理并没有解决我的问题,它是 Eclipse 的一个已知错误。

回答by Philipp Jahoda

"raw" is a (or should be) a folder in your Android Project, containing the file "Jam" in your case. ("Jam" will most likely be a .wav or .mp3 file)

“raw” 是(或应该是)Android 项目中的一个文件夹,在您的案例中包含文件“Jam”。(“果酱”很可能是 .wav 或 .mp3 文件)

Since "raw" cannot be resolved you probably do not have a folder called "raw" in your project. In order to get rid of the error create a folder called "raw" in your Android Project folder.

由于无法解析“raw”,您的项目中可能没有名为“raw”的文件夹。为了消除错误,请在您的 Android 项目文件夹中创建一个名为“raw”的文件夹。

I am not 100% sure about this but I am also quite sure that files inside the "raw" folder cannot contain CAPITAL letters. (only a-z0-9)

我对此不是 100% 确定,但我也很确定“原始”文件夹中的文件不能包含大写字母。(仅 a-z0-9)

回答by Chinmai Kulkarni

  • You need to right-click on res->new->Android resource directory.

  • Select raw in resource type and it automatically selects raw as the directory name.

  • Drag and drop your .mp3 music file inside the res folder. Make sure that it starts with a small letter.

  • 您需要右键单击 res->new->Android 资源目录。

  • 在资源类型中选择 raw ,它会自动选择 raw 作为目录名称。

  • 将您的 .mp3 音乐文件拖放到 res 文件夹中。确保它以小写字母开头。