I have project to make Android Application music Instrument, the Application work like a Piano, if I click ImageButton with SingleTouch his play sound but I want application work with MultiTouch, I don't know to implement in my code ?
Following is my code,
private int mSoundID;
private int mSoundID2;
private int mSoundID3;
private int mSoundID4;
private int mSoundID5;
private int mSoundID6;
private int mSoundID7;
private int mSoundID8;
private int mSoundID9;
private int mSoundID10;
private int mSoundID11;
private int mSoundID12;
private int mSoundID13;
private int mSoundID14;
private int mSoundID15;
private int mSoundID16;
private boolean mSoundLoaded = true;
private float mVolume = 0f;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.talempong);
View view = findViewById(R.id.imagebutton1);
view.setOnTouchListener(this);
View view2 = findViewById(R.id.imagebutton2);
view2.setOnTouchListener(this);
View view3 = findViewById(R.id.imagebutton3);
view3.setOnTouchListener(this);
View view4 = findViewById(R.id.imagebutton4);
view4.setOnTouchListener(this);
View view5 = findViewById(R.id.imagebutton5);
view5.setOnTouchListener(this);
View view6 = findViewById(R.id.imagebutton6);
view6.setOnTouchListener(this);
View view7 = findViewById(R.id.imagebutton7);
view7.setOnTouchListener(this);
View view8 = findViewById(R.id.imagebutton8);
view8.setOnTouchListener(this);
View view9 = findViewById(R.id.imagebutton9);
view9.setOnTouchListener(this);
View view10 = findViewById(R.id.imagebutton10);
view10.setOnTouchListener(this);
View view11 = findViewById(R.id.imagebutton11);
view11.setOnTouchListener(this);
View view12 = findViewById(R.id.imagebutton12);
view12.setOnTouchListener(this);
View view13 = findViewById(R.id.imagebutton13);
view13.setOnTouchListener(this);
View view14 = findViewById(R.id.imagebutton14);
view14.setOnTouchListener(this);
View view15 = findViewById(R.id.imagebutton15);
view15.setOnTouchListener(this);
View view16 = findViewById(R.id.imagebutton16);
view16.setOnTouchListener(this);
// Set the hardware buttons to control the music
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
// Load the sound
mSoundPool = new SoundPool(16, AudioManager.STREAM_MUSIC, 0);
mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status)
{
{
mSoundLoaded = true;
}
}
});
mSoundID = mSoundPool.load(this, R.raw.doo, 1);
mSoundID2 = mSoundPool.load(this, R.raw.re, 1);
mSoundID3 = mSoundPool.load(this, R.raw.mi, 1);
mSoundID4 = mSoundPool.load(this, R.raw.fa, 1);
mSoundID5 = mSoundPool.load(this, R.raw.sol, 1);
mSoundID6 = mSoundPool.load(this, R.raw.la, 1);
mSoundID7 = mSoundPool.load(this, R.raw.si, 1);
mSoundID8 = mSoundPool.load(this, R.raw.do_tinggi, 1);
mSoundID9 = mSoundPool.load(this, R.raw.sol_rendah, 1);
mSoundID10 = mSoundPool.load(this, R.raw.la_rendah, 1);
mSoundID11 = mSoundPool.load(this, R.raw.si_rendah, 1);
mSoundID12 = mSoundPool.load(this, R.raw.re_kres, 1);
mSoundID13 = mSoundPool.load(this, R.raw.fa_kres, 1);
mSoundID14 = mSoundPool.load(this, R.raw.sol_kres, 1);
mSoundID15 = mSoundPool.load(this, R.raw.la_kres, 1);
mSoundID16 = mSoundPool.load(this, R.raw.re_tinggi, 1);
// Getting the user sound settings
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mVolume = actualVolume / maxVolume;
}
//single touch
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN ){
//multitouch
// Is the sound loaded already?
if (mSoundLoaded) {
if (v.getId() == R.id.imagebutton1) {
playSound(mSoundID);
Log.e("Test", "Played mSoundID");
} else if (v.getId() == R.id.imagebutton2) {
playSound(mSoundID2);
Log.e("Test", "Played mSoundID2");
} else if (v.getId() == R.id.imagebutton3) {
playSound(mSoundID3);
Log.e("Test", "Played mSoundID3");
} else if (v.getId() == R.id.imagebutton4) {
playSound(mSoundID4);
Log.e("Test", "Played mSoundID4");
} else if (v.getId() == R.id.imagebutton5) {
playSound(mSoundID5);
Log.e("Test", "Played mSoundID5");
} else if (v.getId() == R.id.imagebutton6) {
playSound(mSoundID6);
Log.e("Test", "Played mSoundID6");
} else if (v.getId() == R.id.imagebutton7) {
playSound(mSoundID7);
Log.e("Test", "Played mSoundID7");
} else if (v.getId() == R.id.imagebutton8) {
playSound(mSoundID8);
Log.e("Test", "Played mSoundID8");
} else if (v.getId() == R.id.imagebutton9) {
playSound(mSoundID9);
Log.e("Test", "Played mSoundID9");
} else if (v.getId() == R.id.imagebutton10) {
playSound(mSoundID10);
Log.e("Test", "Played mSoundID10");
} else if (v.getId() == R.id.imagebutton11) {
playSound(mSoundID11);
Log.e("Test", "Played mSoundID11");
} else if (v.getId() == R.id.imagebutton12) {
playSound(mSoundID12);
Log.e("Test", "Played mSoundID12");
} else if (v.getId() == R.id.imagebutton13) {
playSound(mSoundID13);
Log.e("Test", "Played mSoundID13");
} else if (v.getId() == R.id.imagebutton14) {
playSound(mSoundID14);
Log.e("Test", "Played mSoundID14");
} else if (v.getId() == R.id.imagebutton15) {
playSound(mSoundID15);
Log.e("Test", "Played mSoundID15");
} else if (v.getId() == R.id.imagebutton16) {
playSound(mSoundID16);
Log.e("Test", "Played mSoundID16");
}
}
}
return false;
}
private void playSound(int soundId) {
mSoundPool.play(soundId, mVolume, mVolume, 1, 0, 1f);
}
}