Pregunta

I'm new to Android and I would like to play a sound depending on the button that was pressed by the user.

I managed to play a sound on button press but I had to specify the file that I wanted to be played.

What I want to do is to find a way to dynamically set the R.raw.arthaswhat5 parameter so that it is set up to the last button pressed.

public void listen(View w){
    MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.arthaswhat5);
    mediaPlayer.start();
}

I thought that naming the buttons the same way as my files could help but I don't really understand how this R thing works... I know I can get the v.getId() int to know which button is pressed but I can't use this id to play a sound accordingly...

¿Fue útil?

Solución 2

R.raw like R.id in basic words like pointer to where these values have been stored.

When you save some image or wav file under raw folder, after Project refresh you can call it like R.raw.arthaswhat5 that returns int.

By the same way R.id is generated when you add new GUI element.

There is no dependency between R.raw and R.id. R.raw points to raw folder when R.id points to your view XML.

from `View` you can fetch id to you it for `if` statement or `switch`

like

if (v.getId() == R.id.your_button){ /*...*/}  

[Edit]

if you have > 100 songs I would use assets folder instead raw. Because in raw all names must be lower-case and you can't create there sub-directories. Will be hard to handle and maintain.

Otros consejos

You want to use a Sound Pool http://developer.android.com/reference/android/media/SoundPool.html

In your "res" folder add a folder called "raw" and put your sound files in there. I used .m4a files and it worked for me, but I'm not sure which other formats are supported.

Here's a snippet from a code I used in my app, to play a sound use the following code:

int flip = 1,scratch = 2,wrong = 3,correct = 4,pop = 5;
SoundPool soundPool;
HashMap<Integer, Integer> soundPoolMap;

setVolumeControlStream(AudioManager.STREAM_MUSIC);

soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 100);
soundPoolMap = new HashMap<Integer, Integer>();
soundPoolMap.put(flip, soundPool.load(this, R.raw.flip, 1));
soundPoolMap.put(scratch, soundPool.load(this, R.raw.scratch, 1));
soundPoolMap.put(wrong, soundPool.load(this, R.raw.wrong, 1));
soundPoolMap.put(correct, soundPool.load(this, R.raw.correct, 1));
soundPoolMap.put(pop, soundPool.load(this, R.raw.pop, 1));

soundPool.play(soundPoolMap.get(flip), 1, 1, 1, 0, 1);

Edit: Almost completely neglected a part of your question. You need to use a switch/case scope to determine which button is clicked and apply the correct sound to it accordingly:

public void listen(View v) {
   switch(v.getId()) {
      case (R.id.button1):
         soundPool.play(soundPoolMap.get(flip), 1, 1, 1, 0, 1);
         break;
      case (R.id.button2):
         soundPool.play(soundPoolMap.get(scratch), 1, 1, 1, 0, 1);
         break;
      case (R.id.button3):
         ...
   }
}

Why don't u use a switch-case statement like this?

public void listen(View v){
   MediaPlayer mediaPlayer;
   switch(v.getid()) {
      case (R.id.sound1):
         mediaPlayer = MediaPlayer.create(this, R.raw.arthaswhat5);
         mediaPlayer.start();
         break;
      case (R.id.sound2):
         mediaPlayer = MediaPlayer.create(this, R.raw.arthaswhat6);
         mediaPlayer.start();
         break;
      case (R.id.sound3):
         ...
         ...
         ...
      case (...)
         ...
         ...
         ...
   }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top