如何使用 AudioManager.STREAM_VOICE_CALL 更改通话音量

How to change call volume with AudioManager.STREAM_VOICE_CALL

我有一个通话申请。我创建曲目并播放声音。

AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL, sampleRateInHz, AudioFormat.CHANNEL_CONFIGURATION_MONO, audioFormat, minBufferSize, AudioTrack.MODE_STREAM);

但是当我按下音量按钮时,"media" 的音量发生变化,而不是 "call"。

我可以让音量按钮控制 "call" 音量吗?

我找到了更改 "call" 音量的方法,但我需要使用设备上的音量按钮来完成。

audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, 10, AudioManager.FLAG_SHOW_UI);

我在 IN_CALL 中使用音频管理器 setMode,它适用于我的 VoIP 应用程序。

audioManager.setMode(AudioManager.MODE_IN_CALL);


 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.d(THIS_FILE, "Key down : " + keyCode);
 switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_DOWN:
            case KeyEvent.KEYCODE_VOLUME_UP:

                // Volume has been adjusted by the user.

                Log.d(THIS_FILE, "onKeyDown: Volume button pressed");
                int action = AudioManager.ADJUST_RAISE;
                if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
                    action = AudioManager.ADJUST_LOWER;
                }

                // Detect if ringing
                SipCallSession currentCallInfo = getActiveCallInfo();
                // If not any active call active
                if (currentCallInfo == null && serviceConnected) {
                    break;
                }

                if (service != null) {
                    try {
                        service.adjustVolume(currentCallInfo, action, AudioManager.FLAG_SHOW_UI);
                    } catch (RemoteException e) {
                        Log.e(THIS_FILE, "Can't adjust volume", e);
                    }
                }
                return true;
}

这解决了我的问题。但这是一个技巧。感谢 Mayur Panchal 的想法。

public boolean onKeyUp(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_DOWN:
        case KeyEvent.KEYCODE_VOLUME_UP:
            return true;
    }
    return super.onKeyUp(keyCode, event);
}

public boolean onKeyDown(int keyCode, KeyEvent event) {
     switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_DOWN:
        case KeyEvent.KEYCODE_VOLUME_UP:
            AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            audioManager.adjustStreamVolume(streamType, (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) ? -1 : 1, AudioManager.FLAG_SHOW_UI);
            return true;
    }
    return super.onKeyDown(keyCode, event);
}

我尝试使用 audioManager.setMode(AudioManager.MODE_IN_CALL),但它不适用于我的所有手机。

我在我的 baresip voip 应用程序中使用它:


   override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        when (keyCode) {
            KeyEvent.KEYCODE_VOLUME_DOWN, KeyEvent.KEYCODE_VOLUME_UP -> {
                am.adjustStreamVolume(volumeControlStream,
                        if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
                            AudioManager.ADJUST_LOWER else
                            AudioManager.ADJUST_RAISE,
                        AudioManager.FLAG_SHOW_UI)
                return true
            }
        }
        return super.onKeyDown(keyCode, event)
    }

来电时,我设置

volumeControlStream = AudioManager.STREAM_RING

当通话建立时,我必须设置

volumeControlStream = AudioManager.STREAM_MUSIC

因为某些原因设置为 AudioManager.STREAM_VOICE_CALL 即使在模式为 AudioManager.MODE_IN_COMMUNICATION 时也不起作用。