Playing sound effect (CLICK/NAVIGATION_RIGHT) for button clicks - Android

11,202

Try this:

button_flip.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            button_flip.playSoundEffect(0);
        }
    });

The value 0 stands for CLICK sound. You can see more values here

Share:
11,202
madu
Author by

madu

Updated on June 04, 2022

Comments

  • madu
    madu about 2 years

    I'm trying to use the playSoundEffect () method to play a sound effect when a button is clicked, but so far its' proved very difficult for some reason.

    I have defined the following.

    <ImageButton android:id="@+id/Button_flip" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:text="FLIP!"
    android:src="@drawable/flip" android:soundEffectsEnabled="true">
    </ImageButton>
    

    Then,

    button_flip.playSoundEffect(android.view.SoundEffectConstants.CLICK);
    

    is called in onCreate() method. But I cannot seem to get it to sound when I click the button. What am I missing here? The documentation has nothing much to go on.

    Do I need to define/call in the onClick() method?

    Any help is appreciated.