tags:

views:

55

answers:

1

How do I set up an audiofile to play when a user touches an image.

Where should I store the audio file and what code should I use to actually play the file? I don't want to bring up the MediaPlayer interface or anything like that.

I was thinking of doing it like this:

foo = (ImageView)this.findViewById(R.id.foo);
    foo.setOnClickListener(this);

public void onClick(View v) {
if (foo.isTouched()) {

 playAudioFile();
  }
}

Thanks

A: 

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...

Cristian