-1

I'm really new to Android Studio, and am trying to make a simple app which plays specific sounds when a specific button is pressed. I have looked up many tutorials on YouTube, stack and other websites, but all of them seem to either give me a lot of errors or are too hard for me too understand (as I am too unexperienced). All I have now are a lot of buttons on the screen which do nothing.

So can someone show me how to make something like this: When button 1 is pressed, sound 1 plays, and when button 2 is pressed, sound 2 plays. (And that has to go on like that for about 20/30 buttons and sounds)

Thanks in advance, -Spickle

Spickle
  • 47
  • 10

2 Answers2

0

Create a MediaPlayer mp object and use this in your onClick methods:

mp = MediaPlayer.create(context, R.raw.SoundForSpecificButton);
mp.start();

Then create a raw folder inside a res folder and put there your sounds.

Here is good documentation about this: http://developer.android.com/guide/topics/media/mediaplayer.html#mediaplayer

If you are having problems please put your code here for review.

Spickle
  • 47
  • 10
Krzysztof Majewski
  • 2,494
  • 4
  • 27
  • 51
  • Never mind my last comment, I figured that out (I think). I don't get any errors, except for one: cannot resolve symbol 'context'. What do I do? – Spickle Oct 17 '15 at 19:34
  • Ah, I figured it out. I changed 'context' to 'this' (I remembered that being used in some of the videos I saw) and now it's working! Thank you! – Spickle Oct 17 '15 at 19:44
0

If you want to audio for a very short duration for example a 'click' sound when you press a button look for SoundPool class in android.

Play sound using soundpool example

If you want to stream a bigger audio file you will have to implement MediaPlayer.

Community
  • 1
  • 1
Parag Naik
  • 571
  • 6
  • 6