Skip to main content
se añadieron 326 caracteres en el cuerpo
Origen Enlace
Jorgesys
  • 111.1k
  • 14
  • 55
  • 134

public class MainActivity extends AppCompatActivity {

public class MainActivity extends AppCompatActivity {

    ImageButton tecla1,tecla2,tecla3,tecla4;
    private SoundPool soundPool;
    private int son_tecla1, son_tecla2, son_tecla3, son_tecla4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tecla1 = (ImageButton)findViewById(R.id.ib1);
        tecla2 = (ImageButton)findViewById(R.id.ib2);
        tecla3 = (ImageButton)findViewById(R.id.ib3);
        tecla4 = (ImageButton)findViewById(R.id.ib4);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            soundPool = new SoundPool.Builder().setMaxStreams(4).build();
        }else {
            soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
        }

        son_tecla1=soundPool.load(this, R. raw.t1,1);
        son_tecla2=soundPool.load(this, R. raw.t2,1);
        son_tecla3=soundPool.load(this, R. raw.t3,1);
        son_tecla4=soundPool.load(this, R. raw.t4,1);

        tecla1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent ) {

                if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                    soundPool.play(son_tecla1,1,1,0,0,0);
                }else if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    soundPool.stop(son_tecla1);
                }
                return false;
            }
        });


        tecla2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent ) {

                if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                    soundPool.play(son_tecla2,1,1,0,0,0);
                }else if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    soundPool.stop(son_tecla2);
                }
                return false;
            }
        });


        tecla3.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent ) {

                if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                    soundPool.play(son_tecla3,1,1,0,0,0);
                }else if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    soundPool.stop(son_tecla3);
                }
                return false;
            }
        });


        tecla4.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent ) {

                if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                    soundPool.play(son_tecla4,1,1,0,0,0);
                }else if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    soundPool.stop(son_tecla4);
                }
                return false;
            }
        });

public class MainActivity extends AppCompatActivity {

ImageButton tecla1,tecla2,tecla3,tecla4;
private SoundPool soundPool;
private int son_tecla1, son_tecla2, son_tecla3, son_tecla4;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tecla1 = (ImageButton)findViewById(R.id.ib1);
    tecla2 = (ImageButton)findViewById(R.id.ib2);
    tecla3 = (ImageButton)findViewById(R.id.ib3);
    tecla4 = (ImageButton)findViewById(R.id.ib4);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        soundPool = new SoundPool.Builder().setMaxStreams(4).build();
    }else {
        soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
    }

    son_tecla1=soundPool.load(this, R. raw.t1,1);
    son_tecla2=soundPool.load(this, R. raw.t2,1);
    son_tecla3=soundPool.load(this, R. raw.t3,1);
    son_tecla4=soundPool.load(this, R. raw.t4,1);

    tecla1.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent ) {

            if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                soundPool.play(son_tecla1,1,1,0,0,0);
            }else if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
                soundPool.stop(son_tecla1);
            }
            return false;
        }
    });


    tecla2.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent ) {

            if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                soundPool.play(son_tecla2,1,1,0,0,0);
            }else if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
                soundPool.stop(son_tecla2);
            }
            return false;
        }
    });


    tecla3.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent ) {

            if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                soundPool.play(son_tecla3,1,1,0,0,0);
            }else if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
                soundPool.stop(son_tecla3);
            }
            return false;
        }
    });


    tecla4.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent ) {

            if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                soundPool.play(son_tecla4,1,1,0,0,0);
            }else if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
                soundPool.stop(son_tecla4);
            }
            return false;
        }
    });
public class MainActivity extends AppCompatActivity {

    ImageButton tecla1,tecla2,tecla3,tecla4;
    private SoundPool soundPool;
    private int son_tecla1, son_tecla2, son_tecla3, son_tecla4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tecla1 = (ImageButton)findViewById(R.id.ib1);
        tecla2 = (ImageButton)findViewById(R.id.ib2);
        tecla3 = (ImageButton)findViewById(R.id.ib3);
        tecla4 = (ImageButton)findViewById(R.id.ib4);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            soundPool = new SoundPool.Builder().setMaxStreams(4).build();
        }else {
            soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
        }

        son_tecla1=soundPool.load(this, R. raw.t1,1);
        son_tecla2=soundPool.load(this, R. raw.t2,1);
        son_tecla3=soundPool.load(this, R. raw.t3,1);
        son_tecla4=soundPool.load(this, R. raw.t4,1);

        tecla1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent ) {

                if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                    soundPool.play(son_tecla1,1,1,0,0,0);
                }else if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    soundPool.stop(son_tecla1);
                }
                return false;
            }
        });


        tecla2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent ) {

                if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                    soundPool.play(son_tecla2,1,1,0,0,0);
                }else if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    soundPool.stop(son_tecla2);
                }
                return false;
            }
        });


        tecla3.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent ) {

                if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                    soundPool.play(son_tecla3,1,1,0,0,0);
                }else if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    soundPool.stop(son_tecla3);
                }
                return false;
            }
        });


        tecla4.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent ) {

                if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                    soundPool.play(son_tecla4,1,1,0,0,0);
                }else if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    soundPool.stop(son_tecla4);
                }
                return false;
            }
        });
Origen Enlace
luis
  • 55
  • 3

tocar varios botones con un dedo

estoy queriendo hacer un piano por partes, como podria hacer el efecto de cuando se toca una tecla y dezlizando hacia izquierda o derecha (sin levantar el dedo) suenen las demas notas? introducir la descripción de la imagen aquí

casi utilice el mismo codigo de la botonera. Leyendo por ahi tendria que poner otro if con el evento de MotionEvent.ACTION_MOVE debajo del MotionEvent.ACTION_DOWN ? y mi otra duda es que si no tendria que utilizar MultiTouch en vez de ontouch ya que seria lo logico o no?

public class MainActivity extends AppCompatActivity {

ImageButton tecla1,tecla2,tecla3,tecla4;
private SoundPool soundPool;
private int son_tecla1, son_tecla2, son_tecla3, son_tecla4;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tecla1 = (ImageButton)findViewById(R.id.ib1);
    tecla2 = (ImageButton)findViewById(R.id.ib2);
    tecla3 = (ImageButton)findViewById(R.id.ib3);
    tecla4 = (ImageButton)findViewById(R.id.ib4);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        soundPool = new SoundPool.Builder().setMaxStreams(4).build();
    }else {
        soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
    }

    son_tecla1=soundPool.load(this, R. raw.t1,1);
    son_tecla2=soundPool.load(this, R. raw.t2,1);
    son_tecla3=soundPool.load(this, R. raw.t3,1);
    son_tecla4=soundPool.load(this, R. raw.t4,1);

    tecla1.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent ) {

            if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                soundPool.play(son_tecla1,1,1,0,0,0);
            }else if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
                soundPool.stop(son_tecla1);
            }
            return false;
        }
    });


    tecla2.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent ) {

            if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                soundPool.play(son_tecla2,1,1,0,0,0);
            }else if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
                soundPool.stop(son_tecla2);
            }
            return false;
        }
    });


    tecla3.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent ) {

            if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                soundPool.play(son_tecla3,1,1,0,0,0);
            }else if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
                soundPool.stop(son_tecla3);
            }
            return false;
        }
    });


    tecla4.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent ) {

            if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                soundPool.play(son_tecla4,1,1,0,0,0);
            }else if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
                soundPool.stop(son_tecla4);
            }
            return false;
        }
    });