How do I make an android app which just plays a sound on click of a button ? Suppose, a sound file a.mp3 is inside res\sound\ How should I play it ? Thanks in advance.
Asked
Active
Viewed 92 times
1 Answers
2
First of all you have to create a folder called raw
then put there the music you want to play and then do something like this :
public class TestSound extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btSound = (Button)this.findViewById(R.id.button1);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.musicTest);
btSound .setOnClickListener(new OnClickListener(){
public void onClick(View v) {
mp.start();
}
});
}
Supported Media FormatsAndroid
Hope it works :)

Community
- 1
- 1

Skizo-ozᴉʞS ツ
- 19,464
- 18
- 81
- 148
-
Does it play all the sounds in raw folder ?? – Hello World Aug 19 '15 at 14:48
-
What do you mean? It plays the music that you choose on `R.raw.` – Skizo-ozᴉʞS ツ Aug 19 '15 at 14:48
-
You made my day. Thanks :D – Hello World Aug 19 '15 at 14:50
-
@HelloWorld I've eddited my question to show you the supported media formats on Android. If my answer helped to you don't forget to upvote/mark it as a correct ^^ Happy coding – Skizo-ozᴉʞS ツ Aug 19 '15 at 14:51
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87395/discussion-between-hello-world-and-skizo). – Hello World Aug 19 '15 at 14:54
-
do you need more help @HelloWorld? – Skizo-ozᴉʞS ツ Aug 19 '15 at 15:00