Set audio attributes in SoundPool.Builder class for API 21

I follow a series of lectures on Android programming that has been developed in the pre-API 21 times. Therefore, he tells me that I am creating the SoundPool variable as follows.

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. So, I do 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 type Builder for API 21 and sp is of type SoundPool .

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

 sp21.setAudioAttributes(AudioAttributes.USAGE_MEDIA); 

But Lint marks it in red and says:

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

I know that even if I do not set it to USAGE_MEDIA, it will be installed by default. But I ask for a future link if I need to set it to something else: USAGE_ALARM.

How should I proceed?

Please, help!

I mentioned Audio Attributes , SoundPool , SoundPool.builder and AudioManager .

+6
source share
2 answers

An AudioAttributes instance is created through its AudioAttributes.Builder constructor.

You can use it as follows.

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

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

+10
source

I have something to add. I used SoundPool in my game application to play small and simple ogg audio files. It worked great even on emulators with API 21. Today I decided to change it to use SoundPool.Builder ().

I looked at the Android SoundPool.Builder doc. It says

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

Pay attention to the line "Class Builder for SoundPool objects". Therefore, SoundPool.Builder () creates a SoundPool object. SoundPool () also creates a SoundPool object. So that’s 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; 

The rest of the code (where I load, play, stop, release the sound) remains the same as before. And it works in API 21 and earlier

Hope this helps you all

+9
source

Source: https://habr.com/ru/post/981702/


All Articles