如何使用AudioManager静音听筒?

时间:2018-09-19 07:46:18

标签: java android webrtc

任何人都可以告诉我如何在android(targetSdkVersion 26)中使听筒静音。我尝试过:

audioManager.setSpeakerphoneOn(false);

但是它只会使扬声器静音,而不会使听筒静音。
我也尝试过:

audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
audioManager.setStreamVolume(STREAM_VOICE_CALL, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);

这:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    am.requestAudioFocus(new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE)
              .setAudioAttributes(new AudioAttributes.Builder().setUsage(USAGE_VOICE_COMMUNICATION).build()).build());
    am.adjustVolume(AudioManager.ADJUST_MUTE, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
}

具有相同的效果。

我与webrtc一起工作,我的目标是使输出音频静音/取消静音。非常感谢。

1 个答案:

答案 0 :(得分:0)

Here is how to mute/unmute:

private RTCSession currentSession;

private void setAudioEnabled(boolean isAudioEnabled) {
    if (currentSession != null && currentSession.getMediaStreamManager() != null) {
        currentSession.getMediaStreamManager().getLocalAudioTrack().setEnabled(isAudioEnabled);
    }
}

And also if you want to switch between different audio sources:

public void onSwitchAudio() {
    if (audioManager.getSelectedAudioDevice() != AppRTCAudioManager.AudioDevice.SPEAKER_PHONE) {
        audioManager.selectAudioDevice(AppRTCAudioManager.AudioDevice.SPEAKER_PHONE);
    } else {
        if (audioManager.getAudioDevices().contains(AppRTCAudioManager.AudioDevice.BLUETOOTH)) {
            audioManager.selectAudioDevice(AppRTCAudioManager.AudioDevice.BLUETOOTH);
        } else if (audioManager.getAudioDevices().contains(AppRTCAudioManager.AudioDevice.WIRED_HEADSET)) {
            audioManager.selectAudioDevice(AppRTCAudioManager.AudioDevice.WIRED_HEADSET);
        } else {
            audioManager.selectAudioDevice(AppRTCAudioManager.AudioDevice.EARPIECE);
        }
    }
}