Continuously play sound onTouch ()

I want to continuously play the sound of the onTouch cannon shot. I do this for automatic pistol sounds. So I have a problem with a delay in the audio loop. which does not give the real effect of the automatic cannon sound. My main point is not to drag out when the sound is played over and over. My code.

public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub

    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        mediaPlayer = MediaPlayer.create(RipleFire.this,
                sounds[position]);
        // if (mediaPlayer != null && mediaPlayer.isPlaying()) {
        // mediaPlayer.stop();
        // }
        try {
            mediaPlayer.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        imgflag.setImageResource(pistols[position]);

        imgflag.setImageResource(pistolsAnimations[position]);
        mediaPlayer.start();
        mediaPlayer.setLooping(true);

    } else if (event.getAction() == MotionEvent.ACTION_UP) {
        mediaPlayer.stop();
        mediaPlayer.reset();
        mediaPlayer.release();
        imgflag.setImageResource(pistols[position]);
    }

    return true;
}

      

+3


source to share


4 answers


They have a solution to your question method-1 just you can override the touch method by clicking on the sound.

    @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mPlayer.setLooping(true);
                    mPlayer.start();
                    break;
                case MotionEvent.ACTION_UP:
                    mPlayer.pause();
                    break;
                }
                return true;
            }
    //or
    public boolean onTouch(View v, MotionEvent me) {
    switch(me.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if(!mPlayer.isPlaying()) mPlayer.start();
            break;
        case MotionEvent.ACTION_UP:
            if(mPlayer.isPlaying()) mPlayer.stop();
            break;
    }
}

      

method-2 you can also use runtable protector or run method

Boolean IsPressed = false;
Button mbtn = (Button) findViewById(R.id.mbtn);

      

then add stream, in method run add this code

@Override
public void run(){
      while(!btnIsPressed){
          myfunction();
      }
}

      



now, in the onCreate method, add a listener to change the boolean to true, like for example

mbtn.addOnClickListener( new OnClickListener(){

   @Overrride
   public void onClick(View v){
      btnIsPressed = true;
   }
});

      

Edited: I have another method-3 , here we run the thread whenever you touch the view, it checks isPress or not, if the view is touched, the sound repeats until you release the view from touching.

here i posted all the code for the answer

public class Soundstuff extends Activity implements OnTouchListener {
    SoundPool sP;
    boolean isPress = false;
    int gunshot = 0;
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View v = new View(this);
        setContentView(v);

        v.setOnTouchListener(this);

        sP = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
        gunshot = sP.load(this, R.raw.gunshot, 1);

    }

    private Runnable runnable = new Runnable() {

        @Override
        public void run() {
            /* do what you need to do */
            if (isPress) {
                sP.play(gunshot, 1, 1, 0, 0, 1);
                /* and here comes the "trick" */
                handler.postDelayed(this, 300);
            } else {
            }
        }
    };

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            isPress = true;
            handler.postDelayed(runnable, 300);
            break;

        case MotionEvent.ACTION_UP:
            isPress = false;
            handler.postDelayed(runnable, 300);
            break;
        }

        return true;
    }
}

      

hope it solves your problem. SourceCode

+1


source


use SoundPool instead of MediaPlayer.



SoundPool sP = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
gunshot = sP.load(this, R.raw.gunshot, 1);
sP.play(gunshot, 1, 1, 0, 0, 1);

      

+1


source


agree with @haresh: try this one ...

runOnUiThread(new Runnable() {
                    public void run() {

                        if (pd.isShowing()) {

                            pd.dismiss();
                        }
                        Toast.makeText(getApplicationContext(), "" + msg,
                                Toast.LENGTH_LONG).show();

                    }
                });

      

here I close the progress dialog and show some messages on the screen. You can start or stop your media player this way.

0


source


you can use SoundPool instead of MediaPlayer for better performance.

  • SoundPool plays sound unreasonably.
  • You can play 3+ sounds in parallel.
  • We use setRate over the entire range [0.5f-2.0f].

for sounds like gunshoot, or door knocking, etc., use SoundPool because it has many advantages over MediaPlayer. http://developer.android.com/reference/android/media/SoundPool.html http://examples.javacodegeeks.com/android/android-soundpool-example/

0


source







All Articles