1

Pressing the button the first time stops Sound. But pressing it after that does not have any effect. This is my java code:

public class App extends MultiTouchActivity {

SoundPool sp;
    int  dub1s;

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
     dub1s = sp.load(this, R.raw.dub1, 1);


}

public void dubstep1(View view) {

    sp.stop(dub1s);
    sp.play(dub1s, 1, 1, 1, 0, 1f);
}
Gaurav Sharma
  • 4,032
  • 14
  • 46
  • 72
Tom Jackson
  • 230
  • 3
  • 17

1 Answers1

2

SoundPool.stop() takes the stream id (return value from play), not the sound id (return value from load).

They aren't the same thing.

public class App extends MultiTouchActivity {

    SoundPool sp;
    int  mSoundId;
    int  mStreamId = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
    mSoundId = sp.load(this, R.raw.dub1, 1);
}

public void dubstep1(View view) {
    if(mStreamId != 0) {
        sp.stop(mStreamId);
    }
    mStreamId = sp.play(mSoundId, 1, 1, 1, 0, 1f);
}
Tim
  • 35,413
  • 11
  • 95
  • 121
  • Can you please give an example.. If I put return dub1s = sp.load(this, R.raw.dub1, 1); It doesnt work...It changes public void onCreate to public int OnCreate. – Tom Jackson Oct 23 '12 at 18:35
  • @KristijanZrno Updated. You don't want to return a value from `onCreate`, you want to save the stream id that is returned from the `play` function and use it to stop the sound. – Tim Oct 23 '12 at 18:42