Search code examples
androideventsbuttontouchsoundpool

Android Soundpool - Touch events


I'm making a drumpad app with eight imagebuttons that play the sounds on button press. I have been trying to figure out how to make the press of a button more responsive as the problem is that Soundpool plays the file only when the button is released.

I found out there is a way to make the button react to touch using onTouchEvent, but I have no idea how to implement that to my code. I have tried using both .ogg and .wav filetypes for the sounds, but that's not the issue. I'm also planning to create buttons that loop the soundfile when the key is held down, but my major issue is how to make all the buttons response to touch, not release. The code:

package com.example.user.soundboard;
import android.os.Bundle;
import android.view.View;
import android.view.MotionEvent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity {


SoundPool mySound, mySound2, mySound3, mySound4, mySound5, mySound6, mySound7, mySound8;

int hihatId, kickId, snareId, clapId, snare2Id, hihat2Id, kick2Id, openHatId;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mySound = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
    mySound2 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
    mySound3 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
    mySound4 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
    mySound5 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
    mySound6 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
    mySound7 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
    mySound8 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);

    hihatId = mySound.load(this, R.raw.ogghat30, 1);
    hihat2Id = mySound.load(this, R.raw.ogghihat14, 1);
    kickId = mySound.load(this, R.raw.oggkick, 1);
    kick2Id = mySound.load(this, R.raw.oggkick2, 1);
    snareId = mySound.load(this, R.raw.oggsnare8, 1);
    snare2Id = mySound.load(this, R.raw.oggsnare8, 1);
    clapId = mySound.load(this, R.raw.oggclap5, 1);
    openHatId = mySound.load(this, R.raw.ogghat24, 1);

}

public void playHihat(View view) {
    mySound.play(hihatId, 1, 1, 1, 0, 1);
}

public void playHihat2(View view) {
    mySound.play(hihat2Id, 1, 1, 1, 0, 1);
}

public void playOpenHat(View view) {
    mySound.play(openHatId, 1, 1, 1, 0, 1);
}

public void playKick(View view) {
    mySound.play(kickId, 1, 1, 1, 0, 1);
}

public void playKick2(View view) {
    mySound.play(kick2Id, 1, 1, 1, 0, 1);
}

public void playSnare(View view) {
    mySound.play(snareId, 1, 1, 1, 0, 1);
}

public void playSnare2(View view) {
    mySound.play(snare2Id, 1, 1, 1, 0, 1);
}

public void playClap(View view) {
    mySound.play(clapId, 1, 1, 1, 0, 1);
}

For the ImageButtons I'm currently using onClick method in the Layout.

Thanks for the help!


Solution

  • You can set onTouchEvenListener to your view so then you can play your sounds reacting that touch events.

    Here you have an example:

    yourButtonView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        //Fired when you touch the button
                        break;
    
                    case MotionEvent.ACTION_MOVE:
                        //Fired once you are touching button and you move your finger without release the button
                        break;
    
                    case MotionEvent.ACTION_UP:
                        //Fired when you stop touching the button
                        break;
                }
                return true;
            }
        });