我正在尝试从 android 加载声音。
声音在 res/raw/myownsound.wav
下。
我知道我已经可以使用以下方式加载声音:
soundPool.load(context, R.raw.myownsound, 1)
出于自定义目的,我想使用以下方式加载它:
soundPool.load("res/raw/myownsound", 1)
...但我收到以下错误:error loading res/raw/myownsound
。
我还尝试了以下方法:
soundPool.loadSound("android.resource://upg.GraphismeBase/raw/myownsound", 1)
.. 但我也收到错误消息:error loading android.resource://upg.GraphismeBase/raw/myownsound
soundPool.load(path, priority) 的正确使用方法是什么?
最佳答案
在你的项目中创建文件夹结构
/assets/sounds/data/
然后将您的 wav 文件复制到那里。
// Declare globally if needed
int mySoundId;
SoundPool soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0 );
AssetManager am = this.getAssets();
//Use in whatever method is used to load the sounds
try {
mySoundId = soundPool.load(am.openFd("sounds/data/myownsound"), 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
可以试试这个(不确定它是否有效):
SoundPool mySoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
int myAudioFile = getResId("claps", R.raw.class);
try{
mySoundPool.load(context.getActivity(),myAudioFile,1);
} catch (Exception e){
message = String.valueOf(e);
}
public static int getResId(String variableName, Class<?> c) {
Field field = null;
int resId = 0;
try {
field = c.getField(variableName);
try {
resId = field.getInt(null);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return resId;
}
关于Android Soundpool Load(String path, int priority),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12094370/