0

I am using the following code:

button.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v){
                    AudioAttributes at = new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).setUsage(AudioAttributes.USAGE_NOTIFICATION).build();
                    SoundPool sp = new SoundPool.Builder().setMaxStreams(2).setAudioAttributes(at).build();
                    int sonido = sp.load(reserva.this,R.raw.sound_gracias,1);
                    sp.play(sonido,1,1,1,0,1);
                }
            });

Problem:

When i click the button, i can´t hear any sound (I have multimedia sound at 100%).

This is the sound i am using:

https://freesound.org/people/HoseNoseSounds/sounds/510735/

What can i do to solve this?

Thanks you so much for reading!

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Tomas M
  • 287
  • 4
  • 16
  • I believe `load()` is an asynchronous operation as SoundPool does have an [OnLoadCompleteListener](https://developer.android.com/reference/android/media/SoundPool.OnLoadCompleteListener). So, you just need to wait a little before calling `play()`. – Markus Kauppinen Mar 25 '20 at 13:50
  • So, what would you do to solve this? – Tomas M Mar 25 '20 at 14:19
  • 1
    I'd set up the SoundPool i.e. load the sounds beforehand and not in a button handler, maybe in the Activity's `onCreate()` method for example – Markus Kauppinen Mar 25 '20 at 14:23
  • Basically a duplicate of: [Calling a SoundPool Thead onClickEvent](https://stackoverflow.com/questions/23385968/calling-a-soundpool-thead-onclickevent) – Markus Kauppinen Mar 25 '20 at 14:35

0 Answers0