play sound while button is pressed

All your problems with Audio, Video and Images.

play sound while button is pressed

Postby felito » Wed Mar 09, 2011 11:51 pm

hi guys, i am new in the forum :)

i have this code

Code: Select all
package com.tct.soundTouch;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;

public class main extends Activity implements OnTouchListener {

   private MediaPlayer mp;

   public void onCreate(Bundle savedInstanceState) {

      final Button zero = (Button) this.findViewById(;

      mp = MediaPlayer.create(this, R.raw.sound);


   public boolean onTouch(View v, MotionEvent event) {

      switch (event.getAction()) {

      case MotionEvent.ACTION_DOWN:

      case MotionEvent.ACTION_UP:

      return true;


and it works but not as i expected. The sound plays but only for each time that i press the button. My idea is. While i press the button the sound plays, when i stop the action (finger out of the button) music pause.

Any idea please?

Once Poster
Once Poster
Posts: 1
Joined: Wed Mar 09, 2011 11:44 pm


Re: play sound while button is pressed

Postby raj_J2ME » Fri Mar 11, 2011 10:29 am

I can purpose you one solution to the requirement, please check if this can suites to you,

As soon as you pressed the MotionEvent.ACTION_DOWN, you set the flag and reset the same as soon as you pressed the MotionEvent.ACTION_UP, now keep on playing the media till the time the flag was set.

Please check if this helps you.
Thanks with Regards,
Raj - The Kernel
Master Developer
Master Developer
Posts: 311
Joined: Tue Feb 15, 2011 7:40 am
Location: The Capital of India,


Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 10 guests