在HTC One上以16khz单声道PCM(WAV)录制时的Choppy Audio

时间:2013-07-23 02:55:41

标签: android audio wav pcm htc-android

我正在开发一款需要PCM音频录制16khz / 16bits / mono(1声道)的应用。它在摩托罗拉ATRIX上完美运行,但HTC One上的整个文件记录不稳定。我认为这是因为它仍然试图在立体声录制,但为第二个频道编写空白。如果我用立体声录制它效果很好,但我需要单声道。

有没有人听说过这个问题?由于时间限制,将轨道从立体声混合到单声道不是一种选择。

private static final int        RECORDER_SAMPLERATE = 16000;
private static final int        RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
private static final int        RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;

private void startRecording() 
{

    int bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);

    recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
            RECORDER_SAMPLERATE, RECORDER_CHANNELS,
            RECORDER_AUDIO_ENCODING, bufferSize);

    recorder.startRecording();
    isRecording = true;
    recordingThread = new Thread(new Runnable() 
    {
        public void run() 
        {
            try {
                writeAudioDataToFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }, "AudioRecorder Thread");
    recordingThread.start();
}

1 个答案:

答案 0 :(得分:1)

我能够解决这个问题,并找到了一个在Atrix和HTC One上都能很好用的简单解决方案。我不知道为什么它有效(对此有任何见解会非常感激),但这就是我所做的。

我所做的只是将BufferElements2Rec常量从1024更改为512.常量用于下面的代码中。我想较小的缓冲区大小可以让它正常工作。

这是我在记录线程中调用的writeAudioDataToFile()函数:

private void writeAudioDataToFile() throws IOException 
{
    //create filename
    filePath = generateFilePath();

    //start writing data
    short sData[] = new short[BufferElements2Rec];

    FileOutputStream os = null;
    try {
        os = new FileOutputStream(filePath);
    } catch (Exception e) {
        e.printStackTrace();
    }
    byte[] headerBytes = writeWAVHeader(1);
    os.write(headerBytes, 0, headerBytes.length);
    while (isRecording)
    {
        // gets the voice output from microphone to byte format

        recorder.read(sData, 0, BufferElements2Rec);
        System.out.println("Recording audio to file" + sData.toString());
        try {
            // // writes the data to file from buffer
            byte bData[] = short2byte(sData);
            os.write(bData, 0, BufferElements2Rec * BytesPerElement);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    try {
        os.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}