2014-05-13 90 views
0

我想保存pcm文件格式文件以存储在文件夹中以及要保存在SD卡上的文件夹。到目前为止,我只能在SD卡上保存一个pcm音频文件的文件。我试图在SD卡上存储多个pcm文件,但发生错误。错误是我没有记录文件。每次我录制一些声音时,文件长度都保持为零kb。将多个pcm文件存储在SD卡上的文件夹

private void startRecord() { 



File path = new File(Environment.getExternalStorageDirectory() 
       .getAbsolutePath() + "/MYfolder/"); 
     // File file = new File(path,"myrecording.pcm"); 
     path.mkdirs(); 
     // file = new File(path, "myrecording.pcm"); 
     // // File file = new File(Environment.getExternalStorageDirectory(), 
     // // "test.pcm"); 
     // 
     int sampleFreq = 11025; /* (Integer)spFrequency.getSelectedItem(); */ 

     try { 
      audiofile = File.createTempFile("myrecording", ".pcm", path); 
      // recorder.setOutputFile(audiofile.getAbsolutePath()); 
      // file.createNewFile(); 
      // File.createTempFile("myrecording", ".pcm", file); 

      // OutputStream outputStream = new FileOutputStream(file); 
      OutputStream outputStream = new FileOutputStream(
        audiofile.getAbsoluteFile()); 
      BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
        outputStream); 
      DataOutputStream dataOutputStream = new DataOutputStream(
        bufferedOutputStream); 

      int minBufferSize = AudioRecord 
        .getMinBufferSize(sampleFreq, AudioFormat.CHANNEL_IN_MONO, 
          AudioFormat.ENCODING_PCM_16BIT); 

      short[] audioData = new short[minBufferSize]; 

      audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, 
        sampleFreq, AudioFormat.CHANNEL_IN_MONO, 
        AudioFormat.ENCODING_PCM_16BIT, minBufferSize); 

      audioRecord.startRecording(); 

      while (recording) { 
       int numberOfShort = audioRecord.read(audioData, 0, 
         minBufferSize); 
       for (int i = 0; i < numberOfShort; i++) { 
        dataOutputStream.writeShort(audioData[i]); 
       } 
      } 

      audioRecord.stop(); 
      dataOutputStream.close(); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     addRecordingToMediaLibrary(); 
    } 
    protected void addRecordingToMediaLibrary() { 
      ContentValues values = new ContentValues(4); 
      long current = System.currentTimeMillis(); 
      values.put(MediaStore.Audio.Media.TITLE, "audio" + audiofile.getName()); 
      values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current/1000)); 
      values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/pcm"); 
      values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath()); 
      ContentResolver contentResolver = getContentResolver(); 

      Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
      Uri newUri = contentResolver.insert(base, values); 

      sendStickyBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, 
        newUri)); 
      Toast.makeText(this, "Added File " + newUri, Toast.LENGTH_LONG).show(); 
     } 

我已经编辑我的代码,我想通过内容解析器在特定的文件夹,以节省SD卡的PCM文件,。我得到内存泄漏错误不知道该如何解决。

回答

0

YOUR_FILE_NAME应该指出错误每次使用日期时间设置文件名不同势

private void startRecordinga() { 

    AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, 
      RECORDER_SAMPLERATE, RECORDER_CHANNELS, 
      RECORDER_AUDIO_ENCODING, BufferElements2Rec * BytesPerElement); 

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

    //convert short to byte 
private byte[] short2byte(short[] sData) { 
    int shortArrsize = sData.length; 
    byte[] bytes = new byte[shortArrsize * 2]; 
    for (int i = 0; i < shortArrsize; i++) { 
     bytes[i * 2] = (byte) (sData[i] & 0x00FF); 
     bytes[(i * 2) + 1] = (byte) (sData[i] >> 8); 
     sData[i] = 0; 
    } 
    return bytes; 

} 

private void writeAudioDataToFile() { 
    // Write the output audio in byte 



    String filePath = Environment.getExternalStorageDirectory() 
     .getAbsolutePath()+"/Myfolder/"+YOUR_FILE_NAME+".pcm"; 
    short sData[] = new short[BufferElements2Rec]; 

    FileOutputStream os = null; 
    try { 
     os = new FileOutputStream(filePath); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 

    while (isRecording) { 
     // gets the voice output from microphone to byte format 

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

private void stopRecording() { 
    // stops the recording activity 
    if (null != recorder) { 
     isRecording = false; 
     recorder.stop(); 
     recorder.release(); 
     recorder = null; 
     recordingThread = null; 
    } 
} 
+0

我已经更新了我的代码ü可以现在帮我一下吧。 – zek