Search code examples
androidandroid-mediaplayer

Android buttons issues


I am making an piano app, in which there is nine buttons and when we touch the button each buttons play a different sound. But now the problems are-

1) when we touch the button it play the sound of that button only and it didn't play the sound of next buttons on moving the fingers.

2)If i use ACTION_UP to stop sound then at these event when we lift the finger from button it stop the sound without finishing the complete sound of that button.

   public class Piano_view extends Activity implements View.OnTouchListener {

    Button sound1, sound2, sound3, sound4, sound5, sound6, sound7, sound8,
            sound9;

    MediaPlayer mp1, mp2, mp3, mp4, mp5, mp6, mp7, mp8, mp9;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.piano_view);
        initMpFiles();
        setVolumeControlStream(AudioManager.STREAM_MUSIC);





        sound1 = (Button) findViewById(R.id.one);
        sound2 = (Button) findViewById(R.id.second);
        sound3 = (Button) findViewById(R.id.three);
        sound4 = (Button) findViewById(R.id.four);
        sound5 = (Button) findViewById(R.id.five);
        sound6 = (Button) findViewById(R.id.six);
        sound7 = (Button) findViewById(R.id.seven);
        sound8 = (Button) findViewById(R.id.eight);
        sound9 = (Button) findViewById(R.id.nine);

        sound1.setOnTouchListener(this);
        sound2.setOnTouchListener(this);
        sound3.setOnTouchListener(this);
        sound4.setOnTouchListener(this);
        sound5.setOnTouchListener(this);
        sound6.setOnTouchListener(this);
        sound7.setOnTouchListener(this);
        sound8.setOnTouchListener(this);
        sound9.setOnTouchListener(this);
    }

    private void initMpFiles() {
        mp1 = MediaPlayer.create(Piano_view.this, R.raw.a4);
        mp2 = MediaPlayer.create(Piano_view.this, R.raw.b4);
        mp3 = MediaPlayer.create(Piano_view.this, R.raw.c4);
        mp4 = MediaPlayer.create(Piano_view.this, R.raw.c5);
        mp5 = MediaPlayer.create(Piano_view.this, R.raw.e4);
        mp6 = MediaPlayer.create(Piano_view.this, R.raw.b4);
        mp7 = MediaPlayer.create(Piano_view.this, R.raw.f4);
        mp8 = MediaPlayer.create(Piano_view.this, R.raw.d4);
        mp9 = MediaPlayer.create(Piano_view.this, R.raw.g4);
    }





    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int id = v.getId();
        switch (id) {
            case R.id.one:

                float x = event.getX();
                float y = event.getY();
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    mp1.start();
                    mp1.seekTo(0);
                    v.setPressed(true);
                    return true;
                }

                break;
            case R.id.second:
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    mp2.start();
                    mp2.seekTo(0);
                    v.setPressed(true);
                    return true;
                }
                break;
            case R.id.three:
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    mp3.start();
                    mp3.seekTo(0);
                    v.setPressed(true);
                    return true;
                }
                break;
            case R.id.four:
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    mp4.start();
                    mp4.seekTo(0);
                    v.setPressed(true);
                    return true;
                }
                break;



            case R.id.five:
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    mp5.start();
                    mp5.seekTo(0);

                    v.setPressed(true);
                    return true;
                }
                break;
            case R.id.six:
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    mp6.start();
                  mp6.seekTo(0);
                    v.setPressed(true);
                    return true;
                }
                break;
            case R.id.seven:
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    mp7.start();
                   mp7.seekTo(0);
                    v.setPressed(true);
                    return true;
                }
                break;
            case R.id.eight:
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {

                 mp8.start();
                    mp8.seekTo(0);
                    v.setPressed(true);
                    return true;
                }
                break;
            case R.id.nine:
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    mp9.start();
                    mp9.seekTo(0);
                    v.setPressed(true);
                    return true;
                }


                    break;








        }
        return true;
    }


}

Solution

  • In such case,you should not use Button.Draw a view contains "9 Buttons" and handle the touch event by your self.