音频(录制 - 播放)Android异常回声

时间:2017-02-06 03:32:14

标签: android

我正在使用以下代码来实现实时录制和放大播放系统:

import android.media.*;

class Rec {

static boolean m_isRun = true;

static int buffersize, SAMPLE_RATE = 22050;

public static void loopback()

{
    AudioRecord m_record = null;

    AudioTrack m_track = null;

    // Prepare the AudioRecord & AudioTrack

    try {

        buffersize = AudioRecord.getMinBufferSize(SAMPLE_RATE,
                AudioFormat.CHANNEL_CONFIGURATION_MONO,
                AudioFormat.ENCODING_PCM_16BIT);

        //Log.i(LOG_TAG,"Initializing Audio Record and Audio Playing objects");

        m_record = new AudioRecord(MediaRecorder.AudioSource.MIC,
                SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO,
                AudioFormat.ENCODING_PCM_16BIT, buffersize * 1);

        m_track = new AudioTrack(AudioManager.STREAM_MUSIC,
                SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO,
                AudioFormat.ENCODING_PCM_16BIT, buffersize * 1,
                AudioTrack.MODE_STREAM);

        m_track.setPlaybackRate(SAMPLE_RATE);
    } catch (Throwable t) {
        // Log.e("Error", "Initializing Audio Record and Play objects Failed "+t.getLocalizedMessage());
    }

    m_record.startRecording();
    //Log.i(LOG_TAG,"Audio Recording started");
    m_track.play();
    //Log.i(LOG_TAG,"Audio Playing started");

    byte buffer[] = new byte[buffersize];

    while (m_isRun)

    {
        m_record.read(buffer, 0, buffer.length);

        m_track.write(buffer, 0, buffer.length);

    }

}

public static void do_loopback() {
    new Thread(new Runnable() {
        public void run() {
            loopback();
        }
    }).start();
}
}

我已插入耳机。

如您所见,输入音频来自Mic(即来自智能手机内的录音芯片),而输出音频则路由至耳机(由于STREAM_MUSIC流类型的AudioTrack)。

但我的问题是,它会在播放过程中产生Echo。我认为, Echo并不是因为录制了播放音频,因为播放音频只能通过插入的耳机播放。而且耳机距离录音电话差不多1到2英尺。

那么是什么导致了Echo?

1 个答案:

答案 0 :(得分:2)

尝试将minBuffer和MediaRecorder设置为" AudioFormat.CHANNEL_IN_STEREO"我还将AudioTrack设置为" AudioTrack.PERFORMANCE_MODE_LOW_LATENCY"和#34; AudioFormat.CHANNEL_OUT_STEREO",就我而言,这对我有明确的声音效果。

    boolean isRecording;
private int minBuffer;
private int sampleRate = 44100;

void loop() {
    AudioRecord mAudioRecord = null;
    AudioTrack mAudioTrack = null;
    try {
        minBuffer = AudioRecord.getMinBufferSize(sampleRate,
                AudioFormat.CHANNEL_IN_STEREO,
                AudioFormat.ENCODING_PCM_16BIT);


        Log.d(TAG, "loop: " + minBuffer);

        mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
                sampleRate,
                AudioFormat.CHANNEL_IN_STEREO,
                AudioFormat.ENCODING_PCM_16BIT, minBuffer);


        mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                sampleRate,
                AudioFormat.CHANNEL_OUT_STEREO,
                AudioFormat.ENCODING_PCM_16BIT,
                minBuffer,
                AudioTrack.PERFORMANCE_MODE_LOW_LATENCY);

        mAudioTrack.setPlaybackRate(sampleRate);

    } catch (Exception e) {
        Log.d(TAG, "startStream: " + e.getMessage());
    } finally {
        try {
            mAudioRecord.startRecording();
        } catch (IllegalStateException e) {
            Log.d(TAG, "loop: " + e.getMessage());
        }
    }

    mAudioTrack.play();

    byte[] buffer = new byte[minBuffer];

    while (isRecording) {
        mAudioRecord.read(buffer, 0, buffer.length);

        mAudioTrack.write(buffer, 0, buffer.length);
    }
}