1

I'm trying to have a SoundPool play while a view object is being clicked.

I have successfully implemented it but found out that the sound of the click will lag.

I've been researching on StackOverFlow and on the internet for solutions and the best solution seems to be to have the SoundPool on a separate Thread.

I'm very new to the whole Thread thing and so I don't really know what to do at the moment.

I have followed THIS guide on creating a SoundPool thread, however, I don't know how to access it, or even to assign the clips that I wished to play.

Can someone please give me an example of how I should call the SoundPool thread to play my clip while an object in the view is being clicked.

Please find my onClick code below:

@Override
public boolean onTouch(View v, MotionEvent event) {
    int actionPerformed = event.getAction();

    switch(actionPerformed) {
        case MotionEvent.ACTION_DOWN: {
            if (Math.pow((event.getX() - a_Ball.getX()),2)
                + Math.pow((event.getY() - a_Ball.getY()), 2)
                <= Math.pow(a_Ball.getDiameter(),2)) {
                //pCheck = !pCheck;
                if (pauseBall == false) {
                    SoundPool soundPool = new SoundPool(1,AudioManager.STREAM_MUSIC,1);
                    clickSound = soundPool.load(MainActivity.this, R.raw.click, 1);
                    /*soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
                        public void onLoadComplete(SoundPool soundPool, int sampleId,int status) {
                            soundPool.play(clickSound,1.0f, 1.0f, 0, 0, 1.0f);
                            }
                    });*/


                    //Increase Ball Speed
                    sIncrease = true;
                }
            } else {
                if (pauseBall == false) {
                    //set minus health when not touch on ball
                    health--;
                    TextView txtHealth = (TextView)findViewById(R.id.health);
                    String tempHealth = txtHealth.getText().toString();
                    tempHealth =  getResources().getString(R.string.health) + String.valueOf(health);
                    txtHealth.setText(tempHealth);
                }
            }
            break;
        }
        default:
            return false;
    }
    return true;
}   //onTouch end

Thanks in advance.

Community
  • 1
  • 1
fireboy0526
  • 129
  • 1
  • 1
  • 12

2 Answers2

1

You don't need threads to make sounds without lag. The lag comes from the loading of the sound file, try create the SoundPool and load the sound (soundPool.load) in advance (e.g. onCreate), then just call soundPool.play in your onTouch method.

Nicolas Defranoux
  • 2,646
  • 1
  • 10
  • 13
  • Thank you for your response nicolas. I have move the create and load of soundPool into my onCreate section and calling in my method, however, it still have the lag....the only temporary solution that I am using is having it to continuously looping at mute. I'll try to see if there are any other solution. Thanks for your reply again – fireboy0526 Apr 30 '14 at 12:52
  • That's strange, I use the SoundPool.play method in a onTouch listener without any lag. I have 0 as last parameter to SoundPool constructor, though the documentation says it has no effect. I also setup my SoundPool to play several sounds in parallel (first param, set to 6), as I do play several sounds. This also helps when one sound is not finished and the next one is starting. – Nicolas Defranoux May 01 '14 at 09:29
  • I see, this is some very good info. I'll be sure to give it a try. But some people seems to have the same problem as I do, and by putting it in a thread seems to solve the problem perfectly (for now). I'll do some more investigation into it. Once again, thank you so much for your help – fireboy0526 May 01 '14 at 10:17
0

Ok, so instead of doing it in another class, I decided to do it in one class and created a new thread by myself. Seems to be working well enough.

I'll mark this thread as answer.

fireboy0526
  • 129
  • 1
  • 1
  • 12