Android Soundpool Load(String path,int priority)
作者:互联网
我正在尝试从android加载声音.
声音在res / raw / myownsound.wav下.
我知道我已经可以使用以下方式加载声音:
soundPool.load(context, R.raw.myownsound, 1)
出于自定义目的,我想使用以下方法加载它:
soundPool.load("res/raw/myownsound", 1)
…但是我收到以下错误:加载res / raw / myownsound时出错.
我也尝试过以下方法:
soundPool.loadSound("android.resource://upg.GraphismeBase/raw/myownsound", 1)
..但我也得到一个错误:错误加载android.resource://upg.GraphismeBase/raw/myownsound
使用soundPool.load(路径,优先级)的正确方法是什么?
解决方法:
在项目中创建文件夹结构
/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,resources,audio,soundpool 来源: https://codeday.me/bug/20190716/1480363.html