3
        File pcmFile = new File(mediaPath, TEMP_PCM_FILE_NAME);

        if (pcmFile.exists())
            pcmFile.delete();

        int total = 0;
        mAudioRecordInstance.startRecording();
        try {

            DataOutputStream pcmDataOutputStream = new DataOutputStream(
                    new BufferedOutputStream(new FileOutputStream(pcmFile)));

            while (isRecording) {
                mAudioRecordInstance.read(mBuffer, 0, mBufferSize);
                for (int i = 0; i < mBuffer.length; i++) {
                    Log.d("Capture", "PCM Write:["+i+"]:" + mBuffer[i]);
                    pcmDataOutputStream.writeShort(mBuffer[i]);
                    total++;
                }
            }
            pcmDataOutputStream.close();
        } catch (IOException e) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    DialogCodes e = DialogCodes.ERROR_CREATING_FILE;
                    showDialog(e.getValue());
                    actionButton.performClick();
                }
            });
            return;
        } catch (OutOfMemoryError om) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    DialogCodes e = DialogCodes.OUT_OF_MEMORY;
                    showDialog(e.getValue());
                    System.gc();
                    actionButton.performClick();
                }
            });
        }

        Log.d("Capture", "Stopping recording!!!");
        mAudioRecordInstance.stop();
        Log.d("Capture", "Processing starts");
        short[] shortBuffer = new short[total];

        try {
            DataInputStream pcmDataInputStream = new DataInputStream(
                    new BufferedInputStream(new FileInputStream(pcmFile)));


            for (int j = 0; pcmDataInputStream.available() > 0; j++) {
                shortBuffer[j] = pcmDataInputStream.readShort();
                Log.d("Capture", "PCM Read:[" + j + "]:" + shortBuffer[j] );
            }
            outStream.write(Utilities.shortToBytes(shortBuffer));
            pcmDataInputStream.close();

        } catch (IOException e) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    DialogCodes e = DialogCodes.ERROR_CREATING_FILE;
                    showDialog(e.getValue());
                    outFile = null;
                    actionButton.performClick();
                }
            });
            return;
        } catch (OutOfMemoryError om) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    DialogCodes e = DialogCodes.OUT_OF_MEMORY;
                    showDialog(e.getValue());
                    System.gc();
                    actionButton.performClick();
                }
            });
        }

我正在尝试将 PCM 数据写入临时文件,以便稍后处理它而不会丢失任何可记录的内容。最初我尝试在同一个录制循环中进行处理,但录制的持续时间与实际持续时间不匹配。现在我想要的是从 PCM 文件中读取 short 并将其写入 WAV 文件(如果此问题得到解决,希望稍后处理短数据)和 header。如果我在 Audacity 中打开文件,它就会变成空的。如果我直接写入 WAV 文件而不是临时 PCM 文件,它工作正常。

Other issue is I am using the handler to run a thread in which I update the duration of recording and update VU meter view. I use mBuffer data to display in VU Meter view and is invalidated every second. No synchronization is used on the data but still it effects the recorded duration. Sometimes it comes out to be thrice the original duration.

Questions are (1) Why reading and writing PCM data to temp file is causing WAV file to be empty? Why reading from the unsynchronized short buffer (member variable) in a thread managed by handler is adding duration to WAV data, this happens when I write recorded buffer to WAV file directly?

4

1 回答 1

3

It was all in the header

http://gitorious.org/android-eeepc/base/blobs/48276ab989a4d775961ce30a43635a317052672a/core/java/android/speech/srec/WaveHeader.java

Once I fixed that everything was fine.

于 2013-07-12T10:34:13.673 回答