0

So I have soundpool OnLoadCompleteListener which has this code mStreamId = soundPool.play(sampleId, 1, 1, 1, 0, 1f); and i have 2 buttons that will play diffrent soundpools.I use mStreamId to stop soundpool.My problem is when i click first button and it plays sound and i click the second button it will stop the sound of first button. Here is my code:

    public class MainActivity extends Settings {
        SoundPool mSoundPool;
        int mSoundId;
        int mStreamId = 0;
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
    mSoundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
            mSoundPool
                    .setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
                        public void onLoadComplete(SoundPool soundPool,
                                int sampleId, int status) {
                            mStreamId = soundPool.play(sampleId, 1, 1, 1, 0, 1f);
}
                });

    }
//OnClick
public void button1(View view) {
        if (mStreamId != 0) {
            mSoundPool.stop(mStreamId);
        }

        String path = getFullFilePath(getApplicationContext(), et1.getText()
                .toString());
        mSoundId = mSoundPool.load(path, 1);
    }
public void button2(View view) {
        if (mStreamId != 0) {
            mSoundPool.stop(mStreamId);
        }

        String path = getFullFilePath(getApplicationContext(), et2.getText()
                .toString());
        mSoundId = mSoundPool.load(path, 1);
    }
user1798049
  • 265
  • 1
  • 3
  • 15

1 Answers1

0

Because you told it to. Remove the stop method calls if you want the sounds to play to completion. Soundpool will return the next available stream from the pool when playing a sound.

Dominic Cerisano
  • 3,522
  • 1
  • 31
  • 44