Search code examples
javaandroidbuttontouch

How to use touch listeners and play a sound only when touching a button?


I have an app with a button which plays a sound when clicked and pauses when clicked again. Now I want this button to only play sound when the button is touched and pause when the touch releases. What I mean is: I touch and hold it the song plays, I untouch it and the song stops playing.So the song should only play when the user touches and holds it not when the button is untouched. I even want a custom button(image) to show as long as the touch state is true and as soon as I untouch it, the default custom button is shown. Here is the code to my main activity:

package com.example.firozkaoo2222.myapplication;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import static com.example.firozkaoo2222.myapplication.R.raw.police;

public class MainActivity extends AppCompatActivity {


private MediaPlayer policeSound;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button policeSounds = this.findViewById(R.id.police);

    policeSounds.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            int eventPadTouch = event.getAction();

            switch (eventPadTouch) {

                case MotionEvent.ACTION_DOWN:
                    // start playing sound , in your case:
                    policeSound.start();
                    return true;


                case MotionEvent.ACTION_UP:
                    // stop playing sound , in your case:
                    policeSound.stop();
                    return true;

            }
            return false;
        }
    });
}}

Code for my custom button.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_pressed="true"
    android:drawable="@drawable/button_pressed" />
<item
    android:drawable="@drawable/button_default" />


</selector>

Solution

  • Use this snippet instead of your OnClickListener method:

    policeSounds.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
    
            if (policeSound == null) {
                policeSound = MediaPlayer.create(getApplicationContext(), R.raw.police);
            }
    
            int eventPadTouch = event.getAction();
    
            switch (eventPadTouch) {
    
                case MotionEvent.ACTION_DOWN:
                    // start playing sound , in your case:
                    policeSound.start();
                    return true;
    
    
                case MotionEvent.ACTION_UP:
                    // stop playing sound , in your case:
                    policeSound.pause();
                    return true;
    
            }
            return false;
        }
    }