AudioRecord将原始PCM数据写入/读取到文件

时间:2013-04-07 19:37:25

标签: android audiorecord

        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文件中读取短文并将其写入WAV文件(如果此问题得到解决,希望稍后处理短数据)和标题。如果我在Audacity中打开文件,它就会变空。如果我直接写入WAV文件而不是临时PCM文件,它可以正常工作。

其他问题是我正在使用处理程序来运行一个线程,我在其中更新记录的持续时间并更新VU仪表视图。我使用mBuffer数据在VU Meter视图中显示,并且每秒都无效。没有对数据使用同步但仍影响记录的持续时间。有时候原来的持续时间是三倍。

问题是(1)为什么读取和写入PCM数据到临时文件导致WAV文件为空?为什么从处理程序管理的线程中读取未同步的短缓冲区(成员变量)是为WAV数据添加持续时间,当我直接将记录缓冲区写入WAV文件时会发生这种情况?

1 个答案:

答案 0 :(得分:3)