简体   繁体   English

Android中的原始文件夹

[英]raw folder in Android

I am trying to play sound effects in a application. 我正在尝试在应用程序中播放声音效果。 Everywhere I look, I am being told to add a raw folder in the res directory(working in eclipse). 我到处看的地方都被告知要在res目录中添加一个原始文件夹(在Eclipse中工作)。

First, I add the raw folder under res. 首先,我将原始文件夹添加到res下。 Then I put my .wav files inside. 然后,我将.wav文件放入其中。 When I try to use R.raw.filename in my code, I get an error "raw cannot be resolved". 当我尝试在代码中使用R.raw.filename时,出现错误“无法解决原始”。 So I clean and rebuild the project. 所以我清理并重建项目。 Suddenly, everywhere that I used R.id or R.anything is underlined as an error. 突然,我使用R.id或R.anything的任何地方都被强调为错误。 When I looked up the error, it turns out that it had something to do with Android.R. 当我查找错误时,事实证明它与Android.R有关。 Nothing I did could fix it. 我没有办法修复它。 So I removed the raw folder and rebuilt the project. 因此,我删除了原始文件夹并重建了项目。 All of a sudden, everything is fine. 突然,一切都很好。

I've repeated this process about 4 times and I have no idea why it's such a pain to add the raw folder. 我已经重复了大约4次此过程,但我不知道为什么添加原始文件夹这么麻烦。 Can anyone tell me what I'm doing wrong? 谁能告诉我我在做什么错?

Do any of your .wav files contains invalid characters in their name? 您的.wav文件中是否有任何名称中包含无效字符? Check if maybe the following messages are shown somewhere in your build: 检查以下消息是否可能在您的内部版本中显示:

Unparsed aapt error(s)! 无法解析的aapt错误! Check the console for output. 检查控制台的输出。

and

Invalid file name: must contain only [a-z0-9_.] 无效的文件名:必须仅包含[a-z0-9_。]

Only lowercase characters, digits, underscore and period are valid for the names plus extensions. 名称和扩展名仅小写字母,数字,下划线和句点有效。

Use assets folder and ... 使用assets文件夹并...

InputStream sound = getAssets().open("filename.mp3");

... or raw folder and ... ...或raw文件夹和...

InputStream sound = getResources().openRawResource(R.raw.filename);

Just make sure that the file called filename exists in the directory called raw . 只要确保名为filename存在于raw目录中即可。

If all uses of R.* are underlined, delete the import android.R in the upper part of your class as Maxim mentioned. 如果对R.*所有用法都带有下划线,请删除类android.R的import android.R ,如Maxim所述。

Apparently your activity added import android.R, after that all references with R goes to android framework (android.jar) which doesn't know anything about your raw files. 显然,您的活动添加了import android.R,之后所有带有R的​​引用都转到了对原始文件一无所知的android框架(android.jar)。 Just check imports and if you see android.R, delete it and add com.my_app_namespace.R Should work after that 只需检查导入,如果您看到android.R,请将其删除并添加com.my_app_namespace.R,之后即可使用

我不确定这是否可以作为解决方案,但是当我使用.ogg文件而不是.wav文件时,一切都运行良好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM