gpt4 book ai didi

android - 音频录制和播放环回缓冲区问题,将数据写入音轨时出现问题

转载 作者:行者123 更新时间:2023-11-29 00:41:57 25 4
gpt4 key购买 nike

在这里,我试图一次录制和播放一个声音,它在几秒钟内工作正常,并在缓冲区写入音轨时强制关闭显示错误(m_track.write(缓冲区,0,BUF_SIZE);)。请检查以下代码,并建议我是否正确初始化音轨对象。

    int SAMPLE_RATE = 8000;
int BUF_SIZE = 1024;
byte[] buffer = new byte[BUF_SIZE];

function()
{
try
{
buffersize = AudioRecord
.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);

} catch (Throwable t) {
Log.e("Audio", "Buffer size failed");
}

if (buffersize <= BUF_SIZE) {
buffersize = BUF_SIZE;
}
try {

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

m_track = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, buffersize * 2,
AudioTrack.MODE_STREAM);

m_track.setPlaybackRate(SAMPLE_RATE);
} catch (Throwable t) {
Log.e("Audio", "Audio Failed");
}

m_record.startRecording();
m_track.play();
try {
m_thread = new Runnable() {
public void run() {
while (m_isRun) {

m_record.read(buffer, 0, BUF_SIZE);
m_track.write(buffer, 0, BUF_SIZE);
}
}
};

m_thread.run();
} catch (Exception e) {
Log.e("ERROR", "exception: " + e.getLocalizedMessage());
}

Log.i("D2Record", "loopback exit");
}

最佳答案

我不太熟悉这个主题,但我认为您遇到了缓冲区大小方面的问题。我将大部分代码放在一边,主要集中在缓冲区处理上。

int SAMPLE_RATE = 8000;
int BUF_SIZE = 1024;

int buffersize = AudioRecord
.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
bufferSize = Math.Max(bufferSize, AudioTrack
.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
bufferSize = Math.max(bufferSize, BUF_SIZE);
byte[] buffer = new byte[bufferSize];

m_record = new AudioRecord(MediaRecorder.AudioSource.MIC,
SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT, bufferSize);
m_track = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, bufferSize,
AudioTrack.MODE_STREAM);

m_track.setPlaybackRate(SAMPLE_RATE);
m_record.startRecording();
m_track.play();
while (m_isRun) {
// You should check 'read' for errors after read.
int read = m_record.read(buffer, 0, bufferSize);
// You should check 'wrote' for errors after write.
int wrote = m_track.write(buffer, 0, read);
}

关于android - 音频录制和播放环回缓冲区问题,将数据写入音轨时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8798984/

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