The relationship between the volume of the Android SoundPool.play method and the system volume

To play audio in Android, in addition to using MediaPlayer, you can also use SoundPool. SoundPool uses the concept of sound effect pool to manage multiple short sound effects, which is especially suitable for scenes that require short prompt sounds.

Usually call SoundPool.load to load an audio file, obtain a sound effect id, and then use the play method to play it.

The prototype of SoundPool's play method is as follows:

int play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate);

The second and third parameters of the play method are used to specify the volume of the left and right channels, and the value range is 0.0~1.0. What I want to talk about here is the relationship between this volume value and the system volume.

For example, if you use AudioManager.STREAM_MUSIC or AudioManager.STREAM_VOICE_CALL to play the notification sound, and the media volume or call volume is A ( AudioManager.getStreamVolume(AudioManager.STREAM_MUSIC), then when you use the SoundPool.play method to play the sound effect, the actual volume is equivalent to:

leftVolume * A

Note that the actual volume effect is a product relationship .

The following is a piece of code circulating on the Internet, in which there is an easily overlooked bug in the volume setting:

AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int currentVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
int maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = ((float)currentVolume)/maxVolume;
...
mSoundPool.play(soundID, volume, volume, 1, -1, 1);

The above code calculates a ratio based on the current media volume and the maximum value of the media volume and passes it to the play method as the volume of the left and right channels. The result is that the actual volume is reduced! Especially (Bug), if you set the media volume to 0 before playback starts, after playback starts, you won't hear the sound! At this time, if you adjust the volume with the volume keys, you will not hear the sound! (I just solved a similar bug today)

So, if you want to play sound effects according to the volume set by the user, the correct way is:

mSoundPool.play(soundID, 1, 1, 1, -1, 1);

And if you want to play sound effects at half the volume set by the system, you can use code similar to the following:

mSoundPool.play(soundID, 0.5f, 0.5f, 1, -1, 1);

The Android development document does not have a detailed description of the volume parameters of SoundPool.play, but we need to pay attention to it, otherwise problems will easily occur.


---------------------
Author: foruok
Source: CSDN
Original: https://blog.csdn.net/foruok/article/details/52578930
Copyright statement: This article Original article for the author, please attach the blog post link for reprint!
Content analysis By: CSDN, CNBLOG blog post one-click reprint plug-in

Guess you like

Origin blog.csdn.net/xiaowang_lj/article/details/130898559