利用SoundPool完成小钢琴的项目案例。
SoundPool的主要用法:
public SoundPool soundPool;
public HashMap<Integer,Integer> soundmap=new HashMap<Integer,Integer>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image_white1=(ImageView) findViewById(R.id.mPanoClickWhiteOne);
//调用SoundPool的构造创建SoundPool的对象
soundPool=new SoundPool(10, AudioManager.STREAM_MUSIC, 100);
//调用SoundPool对象的load方法加载声音
soundmap.put(1, soundPool.load(this, R.raw.white1, 1));
//为控件添加触摸事件
image_white1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
soundPool.play((1), 1, 1, 0, 0, 1);//播放第一个按键对应的音乐
image_white1.setBackgroundColor(0x33969696);
}else if(event.getAction()==MotionEvent.ACTION_UP){
image_white1.setBackgroundColor(0x00ffffff);
}
return true;
}
});
}
主要代码参见上传文件
android 小钢琴
猜你喜欢
转载自1960370817.iteye.com/blog/2251931
今日推荐
周排行