如何控制自己的Android应用的声音量

发布于 2025-02-04 06:43:35 字数 253 浏览 0 评论 0原文

我想这个问题的答案一定是典型的。关键是,当您单击它们时,我有一些播放声音的按钮。我想控制声音卷。我如何控制声音卷?我正在使用以下代码:

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

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)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一笔一画续写前缘 2025-02-11 06:43:35

/参考/Android/Media/AudioManager#PlaySoundFect(INT,%20Float

音量 float:声音效果卷。音量值是原始标量,因此UI控件应按对数进行缩放。如果指定了-1的卷,则将使用audiomanager.stream_music流卷减去3DB。注意:此版本适用于具有自己的设置面板用于启用和控制卷的应用程序。

因此,您需要调整第二个参数。我没有使用过它,但是说那是标量,所以我想1是完整的卷吗?它没有提到任何其他值是最大常数或任何其他值。 -1如果很方便的话,您的声音比当前的媒体音量设置更安静。您需要在0到1之间使用不同的值(我想!),看看您需要什么

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文