录制音频,然后使用AudioRecord,AudioTrack.trouble进行播放

时间:2015-04-18 11:37:57

标签: android audio-recording audiorecord audiotrack android-audiorecord

我实时录制了一个录音,然后播放,但是当我开始播放时 它听起来非常嘈杂的声音和我的声音。所以我只想用干净的语气来表达我的声音

即时通过混响,低音增强,均衡器和可视化效果实现此应用

我只是想在没有这些效果的情况下清理声音

至于此应用目标版本至少适用于Android 2.3.3(api 10)

问:我如何修改这个实质性的代码部分? (适用于audioRecord.startRecording,audioTrack.record,audioTrack.write)

这是我的代码段,如下所示:

////////////////////////////////////

private void main() {

        for (;;) {

            Log.d("AFX", "Starting audio thread");
            this.audioRecord.startRecording();
            this.audioTrack.play();

            int i;

            if (!this.running) {
                this.audioRecord.stop();
                this.audioTrack.stop();
                this.audioRecord.release();
                return;
            }

            i = this.audioRecord.read(this.buffer, 0, this.chunkSize);
            Log.v("AudioRecord", "read " + this.chunkSize + "bytes");

            try {

                // Log.d("AFX", "Starting audio thread");
                this.audioRecord.startRecording();
                this.audioTrack.play();
                Log.d("AFX", "Starting audio thread");

                boolean flag = this.running; // df value : false

                if (!flag) {
                    this.audioRecord.stop();
                    this.audioTrack.stop();
                    // this.audioRecord.release(); // added
                    // this.audioTrack.release(); // added
                    Log.d("AFX", "Exiting audio thread");
                    return;
                }

                i = this.audioRecord.read(this.buffer, 0, this.chunkSize);
                Log.v("AudioRecord", "read " + this.chunkSize + "bytes");

                if (i < 0) {
                    Log.e("AFX", "Record error: " + i);
                    this.running = false;
                    continue;
                    // break;
                }
                if (!this.running) {
                    continue;
                    // break;
                }

            } finally {
                this.audioRecord.stop();
                // this.audioRecord.release(); // added
                this.audioTrack.stop();
                // this.audioTrack.release(); // added
            }

            this.audioTrack.write(this.buffer, 0, i);
            // Log.d("AFX", "Starting audio thread");
            // this.audioRecord.startRecording();
            // this.audioTrack.play();
        }
    }

任何想法都会受到赞赏

非常感谢

2 个答案:

答案 0 :(得分:2)

尝试AudioEffects

ar = new AudioRecord(MediaRecorder.AudioSource.VOICE_RECOGNITION, SAMPLE_RATE_IN_HZ, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bs * 10);

        if(AutomaticGainControl.isAvailable())
        {
            AutomaticGainControl agc =AutomaticGainControl.create(ar.getAudioSessionId());
            //agc.g
            Log.d("AudioRecord", "AGC is " + (agc.getEnabled()?"enabled":"disabled"));
            agc.setEnabled(true);
            Log.d("AudioRecord", "AGC is " + (agc.getEnabled()?"enabled":"disabled" +" after trying to enable"));
        }else
        {
            Log.d("AudioRecord", "AGC is unavailable");
        }

        if(NoiseSuppressor.isAvailable()){
            NoiseSuppressor ns = NoiseSuppressor.create(ar.getAudioSessionId());
            Log.d("AudioRecord", "NS is " + (ns.getEnabled()?"enabled":"disabled"));
            ns.setEnabled(true);
            Log.d("AudioRecord", "NS is " + (ns.getEnabled()?"enabled":"disabled" +" after trying to disable"));
        }else
        {
            Log.d("AudioRecord", "NS is unavailable");
        }
        if(AcousticEchoCanceler.isAvailable()){

            AcousticEchoCanceler aec = AcousticEchoCanceler.create(ar.getAudioSessionId());
            Log.d("AudioRecord", "AEC is " + (aec.getEnabled()?"enabled":"disabled"));
            aec.setEnabled(true);
            Log.d("AudioRecord", "AEC is " + (aec.getEnabled()?"enabled":"disabled" +" after trying to disable"));

        }else
        {
            Log.d("AudioRecord", "aec is unavailable");
        }

他们在api 16+上工作,但他们也有Equalizer,Bass boost和你提到的其他东西。在开始录制之前设置它们。

答案 1 :(得分:0)

在您的问题中,您对所遇到的噪音类型有点不确定。 但是,如果您录制并播放相同的声音,您可能会遇到严重的反馈或回声。通常是一声非常响亮的高音。

Android已内置回声消除功能来解决此问题。如果您使用AudioSource.VOICE_COMMUNICATION,则默认启用它,如下所示:

recorder = new AudioRecord( AudioSource.VOICE_COMMUNICATION, 
                            44100,
                            AudioFormat.CHANNEL_IN_MONO,
                            AudioFormat.ENCODING_PCM_16BIT,
                            n );

此外,44100是唯一可以保证适用于所有Android设备的采样率。不要使用任何其他东西,否则你可能会得到奇怪的结果。