-1

I have a button in an activity and when i press it the sound plays. The sound itself is 2 seconds long. And it only plays when i press on the button. I wanna make it that a user can hold down the button and the sound plays till he releases the button. How can i do this? Here's my current code.

package android.app;
import android.app.R;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;



public class activity2 extends Activity{

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);
//back button that takes u to main.xml
    Button next = (Button) findViewById(R.id.Back);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
        Intent intent = new Intent();
        setResult(RESULT_OK, intent);
        finish();
        }
        } );
    //Button that plays sound (whippingsound)
    Button sound = (Button) findViewById(R.id.sound);
        sound.setOnClickListener(new OnClickListener() {
             public void onClick(View v) {
                 MediaPlayer mp = MediaPlayer.create(activity2.this, R.raw.whippingsound);  
                 mp.start();
             }
        } );

    }   
}

Thanks!!!

user1148715
  • 93
  • 1
  • 1
  • 6
  • Possible duplicate: http://stackoverflow.com/questions/5253371/play-sound-while-button-is-pressed-android – Max Jan 16 '12 at 01:38

1 Answers1

2

The solution to your problem is a combination of

Triggering event when Button is pressed down in Android

and

play sound while button is pressed -android.

Namely, you're using an onClickListener instead of onTouchListener.

Try this instead (note: I also moved the media player out so you create it once and use it over and over again).

    public class activity2 extends Activity{
        MediaPlayer mp = null;

        /** Called when the activity is first created. */
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
    //back button that takes u to main.xml
        Button next = (Button) findViewById(R.id.Back);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);
            finish();
            }
            } );
        mp = MediaPlayer.create(activity2.this, R.raw.whippingsound);
        //Button that plays sound (whippingsound)
        Button sound = (Button) findViewById(R.id.sound);
            sound.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                mp.setLooping(true);
                mp.start();
                break;

            case MotionEvent.ACTION_UP:
                mp.pause();
                break;
            }

            return true;
        }
     });
   }
Community
  • 1
  • 1
John O'Connor
  • 5,244
  • 2
  • 24
  • 29
  • im getting - OnTouchListener cannot be resolved to a type - The method setOnTouchListener(View.OnTouchListener) in the type View is not applicable for the arguments (new OnTouchListener(){}) – user1148715 Jan 16 '12 at 04:01
  • I updated the code -- try it with View.OnTouchListener (the OnTouchListener interface is defined in the View class). – John O'Connor Jan 17 '12 at 00:42
  • Gee thanks, except im getting error Syntax error, insert "}" to complete ClassBody for the semi colon on the 2nd line. and an error on the last } on the bottom that says the same error... any help? – user1148715 Jan 17 '12 at 01:50
  • Updated with the correct closing brackets, although if you're using Eclipse, those are automagically highlighted. If you're not, I highly recommend you do -- it makes Android development at least 10x easier. Don't forget to accept this answer if / when it works for you. – John O'Connor Jan 17 '12 at 01:54
  • Everything works fine except the sound doesnt stop.... it just keeps on going. Can i make it that when the user releases his finger it will stop? – user1148715 Jan 17 '12 at 02:15
  • hmmm -- can you post what your class looks like now? Off hand, I think missing a break in your switch / case statement could cause that. Either that, or for some reason the ACTION_DOWN isn't firing. – John O'Connor Jan 17 '12 at 03:00
  • mp = MediaPlayer.create(activity2.this, R.raw.whippingsound); //Button that plays sound (whippingsound) Button sound = (Button) findViewById(R.id.sound); sound.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mp.setLooping(true); mp.start(); break; case MotionEvent.ACTION_UP: mp.pause(); break; } return true; } }); } } – user1148715 Jan 17 '12 at 03:04