WebRTC语音对讲无声音

最近在搞Webrtc对讲的降噪和回音消除,找了代码然后改了非常多。。。。发现一个很无语的Bug:进入程序进行语音对讲,有偶发扬声器无法播放声音,但如果首先进行视频通话,再语音,刚正常。

刚开始怀疑是代码改了哪里改错了,查了很多资料,Webrtc输出的每行日志都排查了,还是不行,后来直接通过查看喇叭设置,发现那个setSpeakerphoneOn已被关闭了,真是Orz.....

然后直接在启动程序时,先检查下喇叭是否关了,如果关了就重新打开,下面附上打开和关闭的代码吧:

打开扬声器:

//检查是否关闭扬声器了,有些机子开机时,默认是关闭状态,此时需要打开,否则出不了声
    private void openSpeaker() {
        try{
            AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            audioManager.setMode(AudioManager.ROUTE_SPEAKER);
            if(!audioManager.isSpeakerphoneOn()) {
                audioManager.setMode(AudioManager.MODE_IN_CALL);
                audioManager.setSpeakerphoneOn(true);
                audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL,
                        audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL ),
                        AudioManager.STREAM_VOICE_CALL);
                Log.e(PeerConnectionHelper.TAG, "single chat room speaker close,now is going to open it..." + audioManager.isSpeakerphoneOn());
            }else{
                Log.e(PeerConnectionHelper.TAG, "single chat room speaker have open..." + audioManager.isSpeakerphoneOn());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

然后关闭扬声器代码:

public void closeSpeaker() {
        try {
            AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            if(audioManager != null) {
                if(audioManager.isSpeakerphoneOn()) {
                    audioManager.setSpeakerphoneOn(false);
                    audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL,audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL ,AudioManager.STREAM_VOICE_CALL);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

你可能感兴趣的:(Android)