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

时间:2019-04-22 04:43:51

标签: android android-audiomanager

我有一个电话申请。我创建音轨并播放声音。

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

但是当我按下音量按钮时,“媒体”的音量会改变,而不是“通话”。

我可以使音量按钮控制“通话”音量吗?

我找到了一种更改“通话”音量的方法,但是我需要使用设备上的音量按钮进行更改。

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

3 个答案:

答案 0 :(得分:1)

我正在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;
}

答案 1 :(得分:1)

这解决了我的问题。但这是一个把戏。感谢 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),但并非在所有手机上都可以使用。

答案 2 :(得分:0)

我在我的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