音频(录制-回放)Android不寻常的回声

时间:2022-09-08 22:38:48

I am using the following code to Implement a live Recording & Playback system :

我正在使用以下代码实现一个实时录制和回放系统:

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();
}
}

I have earphones plugged in.

我把耳机插上电源。

As you can see, Input audio is coming from Mic (i.e from the recording chip within the Smartphone) while the output audio is routed ONLY to earphones (due to STREAM_MUSIC stream type of AudioTrack).

如你所见,输入音频来自于Mic (i)。在智能手机的记录芯片中,输出音频只被路由到耳机(由于音频流的音频流类型)。

But my problem is that, it results in Echo during playback. I think, the Echo is not due to recording of the Playback audio because the Play back audio is played only through earphones plugged in.And also the Earphones are placed nearly 1 to 2 feet apart from the recording Phone.

但我的问题是,它会在回放过程中产生回音。我认为,回声不是由于回放音频的录音,因为回放音频只能通过插入耳机播放。此外,耳机被放置在离录音电话近1到2英尺的地方。

So what is causing the Echo?

是什么导致了回声?

1 个解决方案

#1


1  

Try setting the minBuffer and the MediaRecorder to "AudioFormat.CHANNEL_IN_STEREO" I also set the AudioTrack to "AudioTrack.PERFORMANCE_MODE_LOW_LATENCY" and "AudioFormat.CHANNEL_OUT_STEREO", In my case this work for me with clear sound output.

尝试将minBuffer和MediaRecorder设置为“AudioFormat”。CHANNEL_IN_STEREO也将AudioTrack设置为“AudioTrack”。PERFORMANCE_MODE_LOW_LATENCY”和“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);
    }
}

#1


1  

Try setting the minBuffer and the MediaRecorder to "AudioFormat.CHANNEL_IN_STEREO" I also set the AudioTrack to "AudioTrack.PERFORMANCE_MODE_LOW_LATENCY" and "AudioFormat.CHANNEL_OUT_STEREO", In my case this work for me with clear sound output.

尝试将minBuffer和MediaRecorder设置为“AudioFormat”。CHANNEL_IN_STEREO也将AudioTrack设置为“AudioTrack”。PERFORMANCE_MODE_LOW_LATENCY”和“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);
    }
}