Playing a sound when button clicking in Android

36,907

Solution 1

This won't work for a very simple reason: MyActivity() is never called.

Android will call your Activity's onCreate(), but any other method calls must be done by you. As you never call you MyActivity() method, the button is never given an onClickListener(), and the sound never played. Try using the following code instead:

public class MainActivity extends Activity {

    private static final String TAG = "MyActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.v(TAG, "Initializing sounds...");

        final MediaPlayer mp = MediaPlayer.create(this, R.raw.alarma_67560);

        Button play_button = (Button)this.findViewById(R.id.play_button);
        play_button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.v(TAG, "Playing sound...");
                mp.start();
            }
        });
        Log.v(TAG, "Sounds initialized.");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

Solution 2

You should put your code inside onCreate and after setContentView() since your method/constructor public void MyActivity(Bundle onSavedStateInstance) is never called, therefore your code is never executed. Something like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.v(TAG, "Initializing sounds...");
    final MediaPlayer mp = MediaPlayer.create(this, R.raw.alarma_67560);
    Button play_button = (Button)this.findViewById(R.id.play_button);
    play_button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.v(TAG, "Playing sound...");
            mp.start();
        }
    });
    Log.v(TAG, "Sounds initialized.");
}
Share:
36,907
Cerin
Author by

Cerin

Updated on May 27, 2020

Comments

  • Cerin
    Cerin almost 4 years

    I'm new to Android, and I'm trying to do something simple like play a custom MP3 when a button is clicked. This seems to be a fairly common question, but even though my code follows the examples, and I'm not getting any errors, I don't hear any sound in either the simulator or a real phone.

    My MainActivity.java:

    public class MainActivity extends Activity {
    
        private static final String TAG = "MyActivity";
    
        public void MyActivity(Bundle onSavedStateInstance) {
            Log.v(TAG, "Initializing sounds...");
    
            final MediaPlayer mp = MediaPlayer.create(this, R.raw.alarma_67560);
    
            Button play_button = (Button)this.findViewById(R.id.play_button);
            play_button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Log.v(TAG, "Playing sound...");
                    mp.start();
                }
            });
            Log.v(TAG, "Sounds initialized.");
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    
    }
    

    And my activity_main.xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:padding="@dimen/padding_medium"
            android:text="@string/hello_world"
            tools:context=".MainActivity" />
    
        <Button
            android:id="@+id/play_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/play_button" />
    
    </RelativeLayout>
    

    I'm developing using the Eclipse plugin, which doesn't show any errors and seems to run the app correctly in the simulator or real phone, but when I click the play button, nothing happens. I'm not sure if the simulator supports sound (but I'm assuming it does) and I've confirmed sound is unmuted on my phone.

    What am I doing wrong?

    Also, I'm not seeing my logging statements shown anywhere, either in Eclipse's console or the LogCat panel. Should I be seeing those printed somewhere?