gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 09:37:19 26 4
gpt4 key购买 nike

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

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

我已经插上了耳机。

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

但我的问题是,它会在播放过程中产生回声。我认为,Echo 不是由于录制播放音频造成的,因为播放音频只能通过插入的耳机播放。而且耳机放置在距离录音手机近 1 到 2 英尺的地方。

那么是什么导致了回声?

最佳答案

尝试将 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);
}
}

关于android - 音频(录制 - 回放)Android 异常回声,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42059984/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com