How to Stop all other previous sounds when new sound play in android soundpool?

97 Views Asked by At

I wrote a simple code to build a piano application for android. it works playing notes when I press related buttons but when I press one button after another one, Both notes play together.I want to stop previews button's sound when press new button.simply I want to play one sound at one time. I used soundpool.stop(streamID) but it didn't works. please could anyone explain me how to solve this problem.?

public class MainActivity extends AppCompatActivity {

private Button a,b,c,d,e,f,g;

private SoundPool soundPool;
private int sound_a,sound_b,sound_c,sound_d,sound_e,sound_f,sound_g,sound_h;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    a = (Button) findViewById(R.id.a);
    b = (Button) findViewById(R.id.b);
    c = (Button) findViewById(R.id.c);
    d = (Button) findViewById(R.id.d);
    e = (Button) findViewById(R.id.e);
    f = (Button) findViewById(R.id.f);
    g = (Button) findViewById(R.id.g);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        soundPool = new SoundPool.Builder().setMaxStreams(5).build();
    }else {
        soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC,0);
    }



    sound_a = soundPool.load(this,R.raw.a3,1);
    sound_b = soundPool.load(this,R.raw.b3,1);
    sound_c = soundPool.load(this,R.raw.c3,1);
    sound_d = soundPool.load(this,R.raw.d3,1);
    sound_e = soundPool.load(this,R.raw.e3,1);
    sound_f = soundPool.load(this,R.raw.f3,1);
    sound_g = soundPool.load(this,R.raw.g3,1);

    a.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            soundPool.play(sound_a,1,1,0,0,1);
        }
    });

    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            soundPool.play(sound_b,1,1,0,0,1);
        }
    });


    c.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            soundPool.play(sound_c,1,1,0,0,1);
        }
    });


    d.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            soundPool.play(sound_d,1,1,0,0,1);
        }
    });


    e.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            soundPool.play(sound_e,1,1,0,0,1);
        }
    });


    f.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            soundPool.play(sound_f,1,1,0,0,1);
        }
    });


    g.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            soundPool.play(sound_g,1,1,0,0,1);
        }
    });





}

}

1

There are 1 best solutions below

0
 senkutto On

I found the solution for this.I changed the maxStream value to 1 and problem solved.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    soundPool = new SoundPool.Builder().setMaxStreams(1).build();
}else {
    soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC,0);
}