利用线性布局进行布局,添加<ImageView>并对其设置拉伸,不然在不同API的分辨率下在图片视图下会有缝隙。设置每一个<ImageView>的id。在Activity.java添加如下代码:
private ImageView image_white1;
private ImageView image_white2;
private ImageView image_white3;
private ImageView image_white4;
private ImageView image_white5;
private ImageView image_white6;
private ImageView image_white7;
private ImageView image_white8;
private SoundPool soundpool;
public HashMap<Integer,Integer> soundmap = new HashMap<Integer, Integer>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
soundpool=new SoundPool(10, AudioManager.STREAM_MUSIC, 100);
image_white1 = (ImageView) findViewById(R.id.mPanoClickWhiteOne);
image_white2 = (ImageView) findViewById(R.id.mPanoClickWhiteTwo);
image_white3 = (ImageView) findViewById(R.id.mPanoClickWhiteThree);
image_white4 = (ImageView) findViewById(R.id.mPanoClickWhiteFour);
image_white5 = (ImageView) findViewById(R.id.mPanoClickWhiteFive);
image_white6 = (ImageView) findViewById(R.id.mPanoClickWhiteSix);
image_white7 = (ImageView) findViewById(R.id.mPanoClickWhiteSeven);
image_white8 = (ImageView) findViewById(R.id.mPanoClickWhiteEight);
// 调用SoundPool对象的load()的方法加载声音,这里用了HashMap
soundmap.put(1, soundpool.load(this, R.raw.white1, 1));
soundmap.put(2, soundpool.load(this, R.raw.white2, 2));
soundmap.put(3, soundpool.load(this, R.raw.white3, 3));
soundmap.put(4, soundpool.load(this, R.raw.white4, 4));
soundmap.put(5, soundpool.load(this, R.raw.white5, 5));
soundmap.put(6, soundpool.load(this, R.raw.white6, 6));
soundmap.put(7, soundpool.load(this, R.raw.white7, 7));
soundmap.put(8, soundpool.load(this, R.raw.white8,
);
image_white1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
soundpool.play(soundmap.get(1), 1, 1, 0, 0, 1);// 播放第一个琴键对应的音频
image_white1.setBackgroundColor(0x33969696);
}else
if(event.getAction()==MotionEvent.ACTION_UP){
image_white1.setBackgroundColor(0x00ffffff);
}
return true;
}
});
image_white2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
soundpool.play(soundmap.get(2), 1, 1, 0, 0, 1);// 播放第一个琴键对应的音频
image_white2.setBackgroundColor(0x33969696);
}else
if(event.getAction()==MotionEvent.ACTION_UP){
image_white2.setBackgroundColor(0x00ffffff);
}
return true;
}
});
image_white3.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
soundpool.play(soundmap.get(3), 1, 1, 0, 0, 1);// 播放第一个琴键对应的音频
image_white3.setBackgroundColor(0x33969696);
}else
if(event.getAction()==MotionEvent.ACTION_UP){
image_white3.setBackgroundColor(0x00ffffff);
}
return true;
}
});
image_white4.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
soundpool.play(soundmap.get(4), 1, 1, 0, 0, 1);// 播放第一个琴键对应的音频
image_white4.setBackgroundColor(0x33969696);
}else
if(event.getAction()==MotionEvent.ACTION_UP){
image_white4.setBackgroundColor(0x00ffffff);
}
return true;
}
});
image_white5.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
soundpool.play(soundmap.get(5), 1, 1, 0, 0, 1);// 播放第一个琴键对应的音频
image_white5.setBackgroundColor(0x33969696);
}else
if(event.getAction()==MotionEvent.ACTION_UP){
image_white5.setBackgroundColor(0x00ffffff);
}
return true;
}
});
image_white6.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
soundpool.play(soundmap.get(6), 1, 1, 0, 0, 1);// 播放第一个琴键对应的音频
image_white6.setBackgroundColor(0x33969696);
}else
if(event.getAction()==MotionEvent.ACTION_UP){
image_white6.setBackgroundColor(0x00ffffff);
}
return true;
}
});
image_white7.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
soundpool.play(soundmap.get(7), 1, 1, 0, 0, 1);// 播放第一个琴键对应的音频
image_white7.setBackgroundColor(0x33969696);
}else
if(event.getAction()==MotionEvent.ACTION_UP){
image_white7.setBackgroundColor(0x00ffffff);
}
return true;
}
});
image_white8.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
soundpool.play(soundmap.get(8), 1, 1, 0, 0, 1);// 播放第一个琴键对应的音频
image_white8.setBackgroundColor(0x33969696);
}else
if(event.getAction()==MotionEvent.ACTION_UP){
image_white8.setBackgroundColor(0x00ffffff);
}
return true;
}
});
SoundPool主要用于播放一些较短的声音片段,与MediaPlayer相比,SoundPool的优势在于CPU资源占用量低和反应延迟小。另外,SoundPool还支持自行设置声音的品质、音量、 播放比率等参数。
Soundpool的具体用法自己上网查询就会有详细的写法
分享到:
相关推荐
程序在gec6818平台运行,程序主要功能模拟小钢琴弹奏,有12种音调,可以切换音频种类,弹奏任意曲目。代码简单,都能看懂,需要用到madplay播放器,安装到开发板即可运行播放
android小钢琴程序,转载的
自制符合钢琴键盘宽度的小钢琴。未来版本:。1、有机会实现44键的键盘的控制技术,弹琴感受更加逼真。2、实现按键长按,按下键盘播放音频,松开键盘停止播放的技术。3、实现音频降解,即声音消减不会过于生硬,更...
这是一个简单的java小钢琴代码,很简单很简单。
DIY小钢琴 我的作品---纯手工制作小提琴,DIY小提琴,你一定会喜欢
小钢琴源码是基于Android的小钢琴软件,大家可以看看,希望给大家带来些帮助。
小钢琴源码.zip
基于vb自制电脑键盘小钢琴,压缩包里包含全部所需文件,vb6.0级以上即可正常运行
基于Labview的小钢琴,代码不难就是随便搞搞。
Android 小钢琴源码.rar
Android小钢琴源码.zip
Android 小钢琴源码.zip源码资源下载Android 小钢琴源码.zip源码资源下载
Android源码——小钢琴源码.zip
Android程序研发源码Android 小钢琴源码.rar