15

I am following an Android Programming video lecture series which was designed in the pre-API 21 times. Hence it tells me to create a SoundPool variable in the following manner.

SoundPool sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
//SoundPool(int maxStreams, int streamType, int srcQuality)

However, I want to use this SoundPool for API 21 as well. So, I am doing this:

if((android.os.Build.VERSION.SDK_INT) == 21){
    sp21 = new SoundPool.Builder();
    sp21.setMaxStreams(5);
    sp = sp21.build();
}
else{
    sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
}

sp21 is a variable of Builder type for API 21 and sp is of SoundPool type.

This works very well with my AVD having API 21 and real device having API 19. (Haven't tried with a real device with API 21 but I think it will work well). Now, I want to set the streamType to USAGE_MEDIA in the if-block before sp = sp21.build();. So I type:

sp21.setAudioAttributes(AudioAttributes.USAGE_MEDIA);

But the Lint marks it in red and says:

The method setAudioAttributes(AudioAttributes) in the type SoundPool.Builder is not applicable for the arguments (int)

I know that even if I do not set it to USAGE_MEDIA it will be set to the same by default. But I am asking for future reference if I have to set it to something else like : USAGE_ALARM.

How should I proceed ?

Please Help!

I have referred to Audio Attributes, SoundPool, SoundPool.builder and AudioManager.

Pranit Bankar
  • 453
  • 1
  • 7
  • 21

2 Answers2

21

I have something to add here. I was using SoundPool in my game app to play small and simple ogg audio files. It was working fine even on emulators with API 21. Today I decided to modify it to use SoundPool.Builder().

I looked at Android's SoundPool.Builder document. It says there

public static class
SoundPool.Builder
extends Object
java.lang.Object
↳   android.media.SoundPool.Builder
Class Overview
Builder class for SoundPool objects.

Note the line "Builder class for SoundPool objects." So SoundPool.Builder() creates SoundPool object. SoundPool() also creates SoundPool object. So this is what I did.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

        AudioAttributes audioAttrib = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_GAME)
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .build();
        mSound = new SoundPool.Builder().setAudioAttributes(audioAttrib).setMaxStreams(6).build();
    }
    else {

        mSound = new SoundPool(6, AudioManager.STREAM_MUSIC, 0);
    }

mSound is declared as

    private SoundPool mSound;

Rest of the code (where I load, play, stop, release sound) remains exactly as it was earlier. And it is working in API 21 and earlier versions

Hope this helps you all

Rohit
  • 231
  • 2
  • 2
20

An AudioAttributes instance is built through its builder, AudioAttributes.Builder.

You can use it in the following way.

sp21.setAudioAttributes(new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build());

Ref:https://developer.android.com/reference/android/media/AudioAttributes.html

rmdroid
  • 889
  • 1
  • 6
  • 7