public class SoundPoolDemo extends Activity {
Button btn1,btn2,btn3;
//创建一个SoundPool对象
SoundPool soundPool;
//定义一个HashMap用于存放音频流的ID
HashMapmusicId=new HashMap();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mian);
btn1=(Button)findViewById(R.id.btn1);
btn2=(Button)findViewById(R.id.btn2);
btn3=(Button)findViewById(R.id.btn3);
//初始化soundPool,设置可容纳12个音频流,音频流的质量为5,
soundPool=new SoundPool(12, 0,5);
//通过load方法加载指定音频流,并将返回的音频ID放入musicId中
musicId.put(1, soundPool.load(this, R.raw.awooga, 1));
musicId.put(2, soundPool.load(this, R.raw.evillaugh, 1));
musicId.put(3, soundPool.load(this, R.raw.jackinthebox, 1));
OnClickListener listener=new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn1:
//播放指定的音频流
soundPool.play(musicId.get(1),1,1, 0, 0, 1);
break;
case R.id.btn2:
soundPool.play(musicId.get(2),1,1, 0, 0, 1);
break;
case R.id.btn3:
soundPool.play(musicId.get(3),1,1, 0, 0, 1);
break;
default:
break;
}
}
};
btn1.setOnClickListener(listener);
btn2.setOnClickListener(listener);
btn3.setOnClickListener(listener);
}
}
|