如何在按下 ImageView 时立即启动音频?

How to start audio as soon as ImageView is pressed?

我的代码仅在释放 ImageView 时播放音频。我应该添加什么才能让音频一按下就开始播放?

MediaPlayer mp = null;

public void next (View view) {
    if (mp != null) {
        mp.stop();
        mp.release();
    }
    mp = MediaPlayer.create(this, R.raw.dry);
    mp.start();
}

XML代码:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/seethrough"
    android:clickable="true"
    android:id="@+id/Seethrough"
    android:onClick="next"
    />

你应该使用类似下面的东西

ImageView seethrough= (ImageView)this.findViewById(R.id.Seethrough);
seethrough.setOnTouchListener(new OnTouchListener()
    {
        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            if (event.getAction() == MotionEvent.ACTION_DOWN)
                // this is where you should call next()
            else if (event.getAction() == MotionEvent.ACTION_UP)
                  // this is if you are interested in the release action 
            return false;
        }
    });

复制并粘贴它,它应该可以工作!

ImageView seethrough;    

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
seethrough = (ImageView) findViewById(R.id.Seethrough);
seethrough.setOnTouchListener(this);
}

@Override
public boolean onTouch(View v, MotionEvent event) {

    ImageView view = (ImageView) v;
switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                 if (mp != null) {
                 mp.stop();
                 mp.release();
                 }
                 mp = MediaPlayer.create(this, R.raw.dry);
                 mp.start();
                    break;
                case MotionEvent.ACTION_MOVE:
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_CANCEL:
                    break;
            }
    return true;
}