1. Check out our companion app, Forums for Android! Download from Google Play

Making a button play a sound when pressed?

Discussion in 'Application Development' started by cr5315, Jul 23, 2010.

  1. cr5315

    cr5315 Well-Known Member
    Thread Starter
    68

    Jul 23, 2010
    435
    158
    68
    Narnia
    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
    163

    Jul 13, 2010
    1,934
    459
    163
    Male
    Implement an OnClickListener and put it in the onClick() method.
     

Share This Page

Loading...