音频记录和播放环回缓冲区问题,将数据写入audiotrack时发出问题

时间:2012-01-10 05:32:57

标签: android audio audio-streaming audio-recording

这里我正在尝试一次录制和播放语音,它正常工作几秒钟并强制关闭显示缓冲区写入audiotrack时的错误(m_track.write(buffer,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");
    }

1 个答案:

答案 0 :(得分:2)

我对这个主题并不完全熟悉,但我认为你的缓冲区大小有问题。我将大部分代码留下,主要集中在缓冲区处理上。

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