[英]How to use RAW folder for multiple audio files
I have the following function: 我有以下功能:
strNameValue = prefs.getString("NamePosition", "");
inNameValueConversion = Integer.parseInt(strNameValue);
if (inNameValueConversion == 0) {
DisplayInformation(inNameValueConversion, R.raw.audio01);
}
if (inNameValueConversion == 1) {
DisplayInformation(inNameValueConversion, R.raw.audio02);
}
if (inNameValueConversion == 2) {
DisplayInformation(inNameValueConversion, R.raw.audio03);
}
if (inNameValueConversion == 3) {
DisplayInformation(inNameValueConversion, R.raw.audio04);
}
Because all the audio file starts with audio
and only the number changes at the end I wanted to create a function which allows me to use less code like this: 因为所有的音频文件开头
audio
和数量只有在最后改变我想创造一个功能,让我少用这样的代码:
public void DisplayInformation(int inNum, final int inSoundResource) {
if (inSoundResource < 2) {
strIConv = String.valueOf("0" + inSoundResource);
inConv = Integer.parseInt(strIConv);
int k = R.raw.audio+"inConv";
}
}
I get the following error: audio cannot be resolved or is not a field
我收到以下错误:
audio cannot be resolved or is not a field
How do I edit the above code so I can just use one function instead of using so many IF statement, since it will be 90+ times. 我如何编辑上面的代码,所以我只能使用一个函数而不是使用那么多的IF语句,因为它将超过90次。
You can use getIdentifier(), so your code would look like this: 您可以使用getIdentifier(),因此您的代码应如下所示:
public void displayInformation(int inNum) {
String id = "audio";
// for inNum < 9, we need to add 0, so for example when you pass 0
// id will be 01, not 1
if (inNum < 9) then id += "0";
//based on your code, 0 - audio01, 1 - audio02 etc, so add 1
id += (inNum + 1);
// call getIdentifier with string containing resource name, which are in your raw folder
// and in your package
int k = getResources().getIdentifier(id, "raw", "your.package.name.here");
//k now contains an id of your resource, so do whatever you want with it
}
and your code can then be reduced to this: 然后您的代码可以简化为:
strNameValue = prefs.getString("NamePosition", "");
inNameValueConversion = Integer.parseInt(strNameValue);
displayInformation(inNameValueConversion);
Remember to use your package name in the call to getIdentifier(). 请记住在对getIdentifier()的调用中使用您的包名称。
Docs are here: http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier(java.lang.String , java.lang.String, java.lang.String) 文件在这里: http : //developer.android.com/reference/android/content/res/Resources.html#getIdentifier (java.lang.String,java.lang.String,java.lang.String)
You could try to do it like it is mentioned in this post: https://stackoverflow.com/a/5626631/3178834 您可以尝试像这篇文章中提到的那样去做: https : //stackoverflow.com/a/5626631/3178834
In your case, you have to create an array for all audio files as resources xml and then load it with res.getXml
into java. 在您的情况下,您必须为所有音频文件创建一个数组作为资源xml,然后使用
res.getXml
将其res.getXml
到Java中。 Just follow the link from above. 只需点击上面的链接。
Building R values that way won't work -- you'll need to use reflection instead. 以这种方式建立R值将行不通-您需要使用反射。
For example: 例如:
import java.lang.reflect.Field;
/* ... */
int id = R.id.class.getField("video" + i).getInt(0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.