androidandroid-audiomanager

How to change call volume with AudioManager.STREAM_VOICE_CALL


I have an application for calls. I create track and play sound.

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

But when I press the volume buttons, the volume of the "media" changes, not the "call".

Can I make the volume buttons control the "call" volume?

I found a way to change the "call" volume, but I need to do it using the volume buttons on the device.

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

Solution

  • This solved my problem. But this is a trick. Thank Mayur Panchal for the idea.

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

    I tried to use audioManager.setMode(AudioManager.MODE_IN_CALL), but it didn't work on all my phones.