30

I have the following code for Android which works fine to play a sound once a button is clicked:

Button SoundButton2 = (Button)findViewById(R.id.sound2);
        SoundButton2.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        mSoundManager.playSound(2);

    }
});

My problem is that I want the sound to play immediately upon pressing the button (touch down), not when it is released (touch up). Any ideas on how I can accomplish this?

Mark B
  • 183,023
  • 24
  • 297
  • 295
codeman
  • 8,868
  • 13
  • 50
  • 79

3 Answers3

36

You should do this: b is the button.

b.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN ) {
                    mSoundManager.playSound(2);
                    return true;
                }

                return false;
            }
        });
serg
  • 109,619
  • 77
  • 317
  • 330
Macarse
  • 91,829
  • 44
  • 175
  • 230
  • Still getting these errors: - The type new View.OnTouchListener(){} must implement the inherited abstract method View.OnTouchListener.onTouch(View, MotionEvent) - The method onTouch(View, MotionEvent) of type new View.OnTouchListener(){} must override a superclass method - MotionEvent cannot be resolved to a type – codeman Apr 11 '10 at 01:26
  • 3
    The code supplied in both of these answers appears to be correct. Add an import to `android.view.MotionEvent`. Here is the documentation for `OnTouchListener`: http://developer.android.com/reference/android/view/View.OnTouchListener.html – CommonsWare Apr 11 '10 at 01:39
  • 5
    Nice, got it to work after importing MotionEvent and using this line: if (event.getAction() == MotionEvent.ACTION_DOWN ) { – codeman Apr 11 '10 at 01:53
  • 1
    Thanks a lot sw333t! event.equals(MotionEvent.ACTION_UP) works for me neither, your solution with event.getAction() ==MotionEvent.ACTION_DOWN ) works perfect! – Prexx Jun 13 '12 at 08:59
  • 1
    Edited the answer with sw333t fix – serg Sep 27 '12 at 19:16
  • Would anyone pls explain what the following checks for here: if (event.getAction() == MotionEvent.ACTION_DOWN ) – Noobification Jul 24 '15 at 15:54
  • Worked perfectly for a onTouch + SoundPool – ieselisra Aug 16 '15 at 10:55
23

Maybe using a OnTouchListener? I guess MotionEvent will have some methods for registering a touch on the object.

   button.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
     // TODO Auto-generated method stub
     return false;
    }
   }))
Peterdk
  • 15,625
  • 20
  • 101
  • 140
  • Thanks for the response. Got the following errors when trying to use that code: - The type new View.OnTouchListener(){} must implement the inherited abstract method View.OnTouchListener.onTouch(View, MotionEvent) - The method onTouch(View, MotionEvent) of type new View.OnTouchListener(){} must override a superclass method - MotionEvent cannot be resolved to a type – codeman Apr 11 '10 at 01:15
  • The answer is to change compiler level to 1.6 (http://stackoverflow.com/questions/1678122/must-override-a-superclass-method-errors-after-importing-a-project-into-eclipse) – Nate Parsons Feb 03 '11 at 05:25
3

import android.view.MotionEvent;

tomanesq
  • 31
  • 1