Question

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);
}
Was it helpful?

Solution

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top