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.app.Activity;
import android.media.MediaPlayer;
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) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      final Button zero = (Button) this.findViewById(R.id.button);
      zero.setOnTouchListener(this);

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

   }

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

      switch (event.getAction()) {

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

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

      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?

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

Top

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
raj_J2ME
Master Developer
Master Developer
 
Posts: 311
Joined: Tue Feb 15, 2011 7:40 am
Location: The Capital of India,

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Exabot [Bot] and 9 guests