如何使用AudioManager静音耳机?

问题描述 投票:3回答:1

任何人都可以告诉我如何在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,我的目标是静音/取消静音输出音频。非常感谢你。

java android webrtc
1个回答
0
投票

以下是如何静音/取消静音:

private RTCSession currentSession;

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

此外,如果您想在不同的音频源之间切换:

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);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.