如何使用触摸监听器并仅在触摸按钮时播放声音?

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

我有一个带有按钮的应用程序,单击时播放声音,再次单击时暂停。现在我希望此按钮仅在触摸按钮时播放声音,并在触摸释放时暂停。我的意思是:我触摸并按住它播放歌曲,我松开它并且歌曲停止 playing.So 歌曲应该只在用户触摸并按住它时播放,而不是在按钮未被触摸时播放。只要触摸状态为真,我什至希望显示一个自定义按钮(图像),一旦我取消触摸它,就会显示默认的自定义按钮。 这是我的主要 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;
        }
    });
}}

我的自定义代码 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>

使用此代码段代替您的 OnClickListener 方法:

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;
    }
}

尝试将此添加到您的 onCreate() 方法中:

policeSounds.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN){ //The id check can be ignored since the listener is on the button already
            //play your sound
        } else if (event.getAction() == MotionEvent.ACTION_UP){ //The id check can be ignored since the listener is on the button already
            //stop the sound
        }
        return true; //you need to return `true` otherwise it won't work
    }
});

如果我没理解错的话,你需要使用 setOnTouchListener 而不是 setOnClickListener,如果事件动作是 ACTION_DOWN(按下按钮)就播放声音,当事件动作是 [=14 时停止声音=](触摸被释放):

yourButton.setOnTouchListener((v, event) -> {
    switch (event.getAction()){
        case MotionEvent.ACTION_DOWN:
            play();
            break;

        case MotionEvent.ACTION_UP:
            stop();
            break;

    }
    return false;
});