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

问题描述 投票:0回答:2

我有一个电话申请。我创造了跟踪和播放声音。

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);
android android-audiomanager
2个回答
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
投票

这解决了我的问题。但这是一个伎俩。感谢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),但它并不适用于我的所有手机。

© www.soinside.com 2019 - 2024. All rights reserved.