Search code examples
androidandroid-studiosoundpool

Android SoundPool plays no sound


I want to play a short sound when a button is clicked using SoundPool. But when the button is clicked no sound appears despite the Log telling me it's loaded and "playing". The soundfile is an .ogg in case that's relevant. Here's my code, buttonSound() is called in onCreate().

void buttonSound(){
    AudioAttributes attributes = new AudioAttributes.Builder()
        .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
        .build();

    soundPool = new SoundPool.Builder().setAudioAttributes(attributes).build();

    soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool soundPool, int sampleId,
            int status) {
            Log.d("SOUNDPOOL", "COMPLETE "+button);
        }
    });
    button = soundPool.load(this, R.raw.button, 1);

}


public void onClick(View view) {
    switch (view.getId()) {
        case R.id.button_back:
            // Sound loaded
            if(button != 0) {
                soundPool.play(button, 1f, 1f, 1, 0, 1f);
                Log.d("PLAYING", "PLAYING");
            }
            break;

Solution

  • So apparently AudioAttributes.USAGE_ASSISTANCE_SONIFICATION was the issue, it works fine with AudioAttributes.USAGE_GAME and AudioAttributes.USAGE_MEDIA. I have no idea why, though.