-2

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.

Hello World
  • 84
  • 2
  • 10

1 Answers1

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