如何在我的 Android 应用程序中理智地管理音频音量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/628659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:20:58  来源:igfitidea点击:

How can I manage audio volumes sanely in my Android app?

android

提问by Luke

I have an app that plays intermittent sounds while its activity is open, and the user is always expecting it to make these noises, but unfortunately it is constrained by the music stream volume. The options I have discovered are thus:

我有一个应用程序在其活动打开时播放间歇性声音,用户总是希望它发出这些噪音,但不幸的是它受到音乐流音量的限制。因此,我发现的选项是:

  1. Adjust the music stream's volume, possibly deafening the user if they happen to be playing music at the time.
  2. Call MediaPlayer.setVolume() which is ineffective if the music stream's volume is 0.
  3. Handle volume button presses myself which presents two issues: volume button presses adjust the ringer volume unless my audio is playing, and I have been as of yet unable to catch the onKey event for a volume button press using my OnKeyListener. If there were some way to force the adjustment to apply to the music stream while my activity has focus, or to just catch the button presses myself this would work.
  4. Play a silent looping piece of audio in the background to keep the music stream in context for volume adjustments.
  1. 调整音乐流的音量,如果用户当时正在播放音乐,可能会震耳欲聋。
  2. 如果音乐流的音量为 0,则调用 MediaPlayer.setVolume() 无效。
  3. 处理音量按钮按下自己会出现两个问题:音量按钮按下调整铃声音量,除非我的音频正在播放,而且我还无法使用我的 OnKeyListener 捕获音量按钮按下的 onKey 事件。如果有某种方法可以在我的活动具有焦点时强制调整应用于音乐流,或者只是自己按下按钮,这将起作用。
  4. 在后台播放一段无声的循环音频,使音乐流保持在上下文中以进行音量调整。

I think the 3rd option is probably best since it cedes control of the volume to the user, but if there were some way to just override the system volume in a single mediaplayer instance that would work too.

我认为第三个选项可能是最好的,因为它将音量控制权转让给用户,但是如果有某种方法可以在单个媒体播放器实例中覆盖系统音量也可以工作。

回答by Luke

Got another suggestion via Google Groups that is the platform integrated solution I was looking for and works fine:

通过 Google Groups 获得了另一个建议,这是我正在寻找的平台集成解决方案,并且工作正常:

Please don't handle the volume keys yourself - it is almost impossible to guarantee that you won't break the behavior of the volume keys.

Call this API in your onCreate():

setVolumeControlStream(AudioManager.STREAM_MUSIC);

This tells the AudioManager that when your application has focus, the volume keys should adjust music volume.

请不要自己处理音量键 - 几乎不可能保证您不会破坏音量键的行为。

在您的 onCreate() 中调用此 API:

setVolumeControlStream(AudioManager.STREAM_MUSIC);

这告诉 AudioManager 当您的应用程序获得焦点时,音量键应该调整音乐音量。

回答by Jill

It is usefull of this API: setVolumeControlStream(AudioManager.STREAM_MUSIC);

这个 API 很有用: setVolumeControlStream(AudioManager.STREAM_MUSIC);

Unless this I will spend more time to fix that volume bug.

除非这样我会花更多的时间来修复那个音量错误。

回答by Ben Simms

If you are using the 'OnKeyDown' method, you can get around this issue by doing the following:

如果您使用的是“OnKeyDown”方法,则可以通过执行以下操作来解决此问题:

public boolean onKeyDown(int keyCode, KeyEvent event) {    
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
                setVolumeControlStream(AudioManager.STREAM_MUSIC);}}

回答by Naresh Prajapati

   var audioManager:AudioManager?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        registerReceiver(mVolumeReceiver, IntentFilter("android.media.VOLUME_CHANGED_ACTION"))
        audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
    }

    private var mVolumeReceiver: BroadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {

            if (intent.action == "android.media.VOLUME_CHANGED_ACTION") {
                var currentVolume = audioManager?.getStreamVolume(AudioManager.STREAM_MUSIC)
            }
        }
    }

回答by Hitendra

This won't help you if you have override onKeyDown(int keycode,KeyEvent event) method of an activity.

如果您覆盖了活动的 onKeyDown(int keycode,KeyEvent event) 方法,这将无济于事。