Android: Playing an audio clip onClick

This won’t create a bring up the MediaPlayer interface… it will just play the sound you want.

Button boton = (Button) findViewById(R.id.boton);
boton.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
  MediaPlayer mp = MediaPlayer.create(TestSonido.this, R.raw.slayer);  
  mp.start();
 }
});

In this case, R.raw.slayer represents an audio file called slayer.mp3 that is stored in the res/raw/ folder and once you click the button the droid will rock you…

Leave a Comment