SoundPool播放Assets资源代码:
protected SoundPool soundPool=null;
protected HashMap<String, Integer> soundMap = new HashMap<String, Integer>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
try {
Thread.sleep(2000);//线程等待2秒,不能加载后立马播放
soundPool.play(soundMap.get("f5"), 1, 1, 0, 0, 1);
} catch (Exception e) {
e.printStackTrace();
}
}
private void init(){
try {
String[] files=this.getResources().getAssets().list("wav");
soundPool=new SoundPool(files.length, AudioManager.STREAM_MUSIC, 0);
for(int i=0;i<files.length;i++){
if(files[i].contains(".wav")){
String filename=files[i].substring(0,files[i].indexOf("."));//获取文件名称
soundMap.put(filename, soundPool.load(getAssets().openFd("wav/"+files[i]), 1));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}