0

Hi I'm working on an android Java app where I need to play three different sounds with soundpool at the same time using three different buttons.**code in picture **. I loaded sounds into three buttons but when I click all three at time no sound is played. But it works when i click two buttons. Is there any way to do this.

BoomPsy
  • 1
  • 1
  • 1
    put some part of your code – Abhishek May 17 '18 at 03:09
  • please put all logic which is inside of three buttons click along with setting ClickListener lines in your question. OR Look https://stackoverflow.com/help/how-to-ask – Abhishek May 17 '18 at 03:40
  • button1.setOnClickListener(new OnClickListener(){ public void onClick(View ,view) {soundPool.play(button_1,1,1,0,0,1);} }); button2.setOnClickListener(new OnClickListener(){ public void onClick(View ,view) {soundPool.play(button_2,1,1,0,0,1);} }); button3.setOnClickListener(new OnClickListener(){ – BoomPsy May 17 '18 at 03:43

1 Answers1

0

Use .setMaxStreams(int maxStreams) inside SoundPool.Builder() method. As this works on api 21 or higher, so you need to set the condition. i.e.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // lollipop is constant for api 21
        AudioAttributes audioAttributes = new AudioAttributes.Builder() // using instance of audio attributes
                .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) // different usage types, press CTRL+B & we get to it's declaration, and we see the description of usage types.
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) 
                .build();
    soundPool = new SoundPool.Builder()
        .setMaxStreams(10) //  Sets the maximum of number of simultaneous streams 
                            // that can be played simultaneously.
        .setAudioAttributes(audioAttributes)
        .build();
    }
       else{
         soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
    }
dvlpr
  • 1
  • Yeah I used that, everything is fine with sound but when I click three buttons at a time OnClickLister doesn't work – BoomPsy May 17 '18 at 03:57
  • Create View otherwise, IT WILL CRASH when you will click on any button. Generate playSound() method and add case switch for certain views by getting their id's. – dvlpr May 17 '18 at 04:17
  • Yeah I used that switch statement with view.getId() it work but problem is multi-click(clicking three buttons at a time) doesn't work. – BoomPsy May 17 '18 at 04:24
  • Have you also used soundPool.autoPause(); inside the case button id of your view.getId()? [Here soundPool is the declared var of SoundPool i.e. private SoundPool soundPool;] – dvlpr May 17 '18 at 04:32