public class MainActivity extends Activity {
private SoundPool soundPool;
private Map
<Integer, Integer
> soundMap
;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1
=(Button)findViewById
(R.
id.
button1);
button2
=(Button)findViewById
(R.
id.
button2);
button3
=(Button)findViewById
(R.
id.
button3);
button4
=(Button)findViewById
(R.
id.
button4);
button5
=(Button)findViewById
(R.
id.
button5);
//创建一个SoundPool对象,该对象可以容纳5个音频流
soundPool=new SoundPool(5,AudioManager.STREAM_MUSIC,0);
soundMap
=new HashMap
<Integer, Integer
>();
soundMap.put(1, soundPool.load(MainActivity.this, R.raw.ir_begin, 1));
soundMap.put(2, soundPool.load(MainActivity.this, R.raw.ir_end, 1));
soundMap.put(3, soundPool.load(MainActivity.this, R.raw.ir_inter, 1));
soundMap.put(4, soundPool.load(MainActivity.this, R.raw.tada, 1));
soundMap.put(5, soundPool.load(MainActivity.this, R.raw.zhong, 1));
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick
(View arg0
) {
// TODO Auto-generated method stub
soundPool.play(soundMap.get(1), 1, 1, 0, 0, 1);
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick
(View arg0
) {
// TODO Auto-generated method stub
soundPool.play(soundMap.get(2), 1, 1, 0, 0, 1);
}
});
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick
(View arg0
) {
// TODO Auto-generated method stub
soundPool.play(soundMap.get(3), 1, 1, 0, 0, 1);
}
});
button4.setOnClickListener(new OnClickListener() {
@Override
public void onClick
(View arg0
) {
// TODO Auto-generated method stub
soundPool.play(soundMap.get(4), 1, 1, 0, 0, 1);
}
});
button5.setOnClickListener(new OnClickListener() {
@Override
public void onClick
(View arg0
) {
// TODO Auto-generated method stub
soundPool.play(soundMap.get(5), 1, 1, 0, 0, 1);
}
});
}
@Override
public boolean onKeyDown
(int keyCode,
KeyEvent event
) {
// TODO Auto-generated method stub
soundPool.play(soundMap.get(5), 1, 1, 0, 0, 1);
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu
(Menu menu
) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
//java/6590