Home > other >  how to control the sound volume of my own android app
how to control the sound volume of my own android app

Time:06-05

I imagine the answer to this question must be something quite typical. The point is that I have some buttons that play sounds when you click on them. I want to control the sound volume. How do I control the sound volume? I am using the following code:

private fun playSound() {
  val sound = getSystemService(Context.AUDIO_SERVICE) as AudioManager 
  sound.playSoundEffect(AudioManager.FX_KEY_CLICK, 1f)
}

CodePudding user response:

https://developer.android.com/reference/android/media/AudioManager#playSoundEffect(int, float)

volume float: Sound effect volume. The volume value is a raw scalar so UI controls should be scaled logarithmically. If a volume of -1 is specified, the AudioManager.STREAM_MUSIC stream volume minus 3dB will be used. NOTE: This version is for applications that have their own settings panel for enabling and controlling volume.

So you need to adjust that second parameter. I haven't used it, but it says that's a scalar so I'd imagine 1 is full volume? It doesn't mention any other value as a max constant or anything. -1 gives you a sound a little quieter than the current media volume setting, if that's convenient. You'll need to play around with different values between 0 and 1 (I assume!) and see what you need

  • Related