2013-04-07 100 views
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文件中读取简短的内容,并将其写入WAV文件(如果此问题已解决,则希望稍后处理短数据)。如果我在Audacity中打开该文件,它将变为空白。如果我直接写入WAV文件而不是临时PCM文件,它可以正常工作。AudioRecord将原始PCM数据写入/读取到文件

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

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

+0

你的问题是什么? – Axarydax 2013-04-07 20:21:49

+0

刚刚计算出来,由于头文件被追加时文件大小为44,文件头没有正确更新。修改它以在处理结束时更新标题。 – 2013-05-02 19:02:21

回答