Making a button play a sound when pressed?


Last Updated:

  1. cr5315

    cr5315 Well-Known Member Developer This Topic's Starter

    Joined:
    Jul 23, 2010
    Messages:
    435
    Likes Received:
    158
    I'm making a very simple app, and I have a button that is supposed to play a sound when pressed. I've tried many things to get it to work, but no success. Here is my code

    Code (Text):
    1. package com.androidbook.droid1;
    2.  
    3. import android.app.Activity;
    4. import android.media.MediaPlayer;
    5. import android.os.Bundle;
    6.  
    7. public class DroidActivity extends Activity {
    8.     /** Called when the activity is first created. */
    9.     @Override
    10.     public void onCreate(Bundle savedInstanceState) {
    11.         super.onCreate(savedInstanceState);
    12.         setContentView(R.layout.main);
    13.         MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.choose);
    14.         mp.start();
    15.     }
    16.    
    17. }
    This makes the choose sound play when the app is opened, and it is supposed to, but I'm not sure how to make a button play a different sound.
     

    Advertisement
  2. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    Implement an OnClickListener and put it in the onClick() method.
     

Share This Page

Loading...