2012-08-08 64 views
2

我试图将音频&存储到SD卡以及发送到服务器。当我试图播放录制的声音时,但并非如我所记录的那样。如果我录制10分钟的声音,它将播放4分钟,即有人按下快速前进按钮并发出一些嘈杂的声音。我不明白我出错的地方。有人可以告诉我如何解决这个问题(我应该播放多少记录,即记录10分钟,然后应该只播放10分钟)。Android:使用audiorecord类录制音频播放为快速转发

这里是代码..张贴散装代码抱歉..

public class Audio_Call extends Activity { 
private static final int RECORDER_BPP = 16; 
private static final String AUDIO_RECORDER_FILE_EXT_WAV = "AudioRecorder.wav"; 
private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder"; 
private static final String AUDIO_RECORDER_TEMP_FILE = "record_temp.raw"; 
private static final int RECORDER_SAMPLERATE = 8000; 
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO; 
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT; 

private AudioRecord recorder = null; 
// private int bufferSize = 200000; 
private int bufferSize = 0; 
short[] buffer; 
private Thread recordingThread = null; 
private boolean isRecording = false; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.audio_call); 

    setButtonHandlers(); 
    enableButtons(false); 

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

} 

private void setButtonHandlers() { 
    ((Button) findViewById(R.id.btnStart)).setOnClickListener(btnClick); 
    ((Button) findViewById(R.id.btnStop)).setOnClickListener(btnClick); 
} 

private void enableButton(int id, boolean isEnable) { 
    ((Button) findViewById(id)).setEnabled(isEnable); 
} 

private void enableButtons(boolean isRecording) { 
    enableButton(R.id.btnStart, !isRecording); 
    enableButton(R.id.btnStop, isRecording); 
} 

// stores the file into the SDCARD 
private String getFilename() { 
    System.out.println("---3---"); 
    String filepath = Environment.getExternalStorageDirectory().getPath(); 
    File file = new File(filepath, AUDIO_RECORDER_FOLDER); 

    if (!file.exists()) { 
     file.mkdirs(); 
    } 

    return (file.getAbsolutePath() + "/" + AUDIO_RECORDER_FILE_EXT_WAV); 
} 

// stores the file into the SDCARD 
private String getTempFilename() { 
    // Creates the temp file to store buffer 
    System.out.println("---4-1--"); 
    String filepath = Environment.getExternalStorageDirectory().getPath(); 
    System.out.println("---4-2--"); 
    File file = new File(filepath, AUDIO_RECORDER_FOLDER); 
    System.out.println("---4-3--"); 

    if (!file.exists()) { 
     file.mkdirs(); 
    } 

    File tempFile = new File(filepath, AUDIO_RECORDER_TEMP_FILE); 
    System.out.println("---4-4--"); 

    if (tempFile.exists()) 
     tempFile.delete(); 
    System.out.println("---4-5--"); 
    return (file.getAbsolutePath() + "/" + AUDIO_RECORDER_TEMP_FILE); 

} 

private void startRecording() { 

    int buffercount = 4088/bufferSize; 
    if (buffercount < 1) 
     buffercount = 1; 

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

    buffer = new short[4088]; 

    recorder.startRecording(); 

    isRecording = true; 

    recordingThread = new Thread(new Runnable() { 

     @Override 
     public void run() { 

      writeAudioDataToFile(); 

     } 
    }, "AudioRecorder Thread"); 

    recordingThread.start(); 

} 

private void writeAudioDataToFile() { 

    // Write the output audio in byte 
    byte data[] = new byte[bufferSize]; 

    String filename = getTempFilename(); 
    // 
    FileOutputStream os = null; 
    // 
    try { 
     // 
     os = new FileOutputStream(filename); 
     // 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 

    int read = 0; 


    // if (null != os) { 
    while (isRecording) { 
     // gets the voice output from microphone to byte format 
     recorder.read(buffer, 0, buffer.length); 
     // read = recorder.read(data, 0, 6144); 

     if (AudioRecord.ERROR_INVALID_OPERATION != read) { 
      try { 
       // // writes the data to file from buffer 
       // // stores the voice buffer 

       // short[] shorts = new short[bytes.length/2]; 
       // to turn bytes to shorts as either big endian or little 
       // endian. 
       // ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts); 

       // to turn shorts back to bytes. 
       byte[] bytes2 = new byte[buffer.length * 2]; 
       ByteBuffer.wrap(bytes2).order(ByteOrder.LITTLE_ENDIAN) 
         .asShortBuffer().put(buffer); 

       os.write(bytes2); 
       SendAudio(buffer); 
      } 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; 
    } 

    // copy the recorded file to original copy & delete the recorded copy 
    copyWaveFile(getTempFilename(), getFilename()); 
    deleteTempFile(); 

} 

private void deleteTempFile() { 
    File file = new File(getTempFilename()); 

    file.delete(); 
} 

private void copyWaveFile(String inFilename, String outFilename) { 
    System.out.println("---8---"); 
    FileInputStream in = null; 
    FileOutputStream out = null; 
    long totalAudioLen = 0; 
    long totalDataLen = totalAudioLen + 36; 
    long longSampleRate = RECORDER_SAMPLERATE; 
    int channels = 2; 
    long byteRate = RECORDER_BPP * RECORDER_SAMPLERATE * channels/8; 

    byte[] data = new byte[bufferSize]; 

    try { 
     in = new FileInputStream(inFilename); 
     out = new FileOutputStream(outFilename); 
     totalAudioLen = in.getChannel().size(); 
     totalDataLen = totalAudioLen + 36; 

     AppLog.logString("File size: " + totalDataLen); 

     WriteWaveFileHeader(out, totalAudioLen, totalDataLen, 
       longSampleRate, channels, byteRate); 
     byte[] bytes2 = new byte[buffer.length * 2]; 
     ByteBuffer.wrap(bytes2).order(ByteOrder.LITTLE_ENDIAN) 
       .asShortBuffer().put(buffer); 
     while (in.read(bytes2) != -1) { 
      out.write(bytes2); 
     } 

     in.close(); 
     out.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

private void WriteWaveFileHeader(FileOutputStream out, long totalAudioLen, 
     long totalDataLen, long longSampleRate, int channels, long byteRate) 
     throws IOException { 
    System.out.println("---9---"); 
    byte[] header = new byte[4088]; 

    header[0] = 'R'; // RIFF/WAVE header 
    header[1] = 'I'; 
    header[2] = 'F'; 
    header[3] = 'F'; 
    header[4] = (byte) (totalDataLen & 0xff); 
    header[5] = (byte) ((totalDataLen >> 8) & 0xff); 
    header[6] = (byte) ((totalDataLen >> 16) & 0xff); 
    header[7] = (byte) ((totalDataLen >> 24) & 0xff); 
    header[8] = 'W'; 
    header[9] = 'A'; 
    header[10] = 'V'; 
    header[11] = 'E'; 
    header[12] = 'f'; // 'fmt ' chunk 
    header[13] = 'm'; 
    header[14] = 't'; 
    header[15] = ' '; 
    header[16] = 16; // 4 bytes: size of 'fmt ' chunk 
    header[17] = 0; 
    header[18] = 0; 
    header[19] = 0; 
    header[20] = 1; // format = 1 
    header[21] = 0; 
    header[22] = (byte) channels; 
    header[23] = 0; 
    header[24] = (byte) (longSampleRate & 0xff); 
    header[25] = (byte) ((longSampleRate >> 8) & 0xff); 
    header[26] = (byte) ((longSampleRate >> 16) & 0xff); 
    header[27] = (byte) ((longSampleRate >> 24) & 0xff); 
    header[28] = (byte) (byteRate & 0xff); 
    header[29] = (byte) ((byteRate >> 8) & 0xff); 
    header[30] = (byte) ((byteRate >> 16) & 0xff); 
    header[31] = (byte) ((byteRate >> 24) & 0xff); 
    header[32] = (byte) (2 * 16/8); // block align 
    header[33] = 0; 
    header[34] = RECORDER_BPP; // bits per sample 
    header[35] = 0; 
    header[36] = 'd'; 
    header[37] = 'a'; 
    header[38] = 't'; 
    header[39] = 'a'; 
    header[40] = (byte) (totalAudioLen & 0xff); 
    header[41] = (byte) ((totalAudioLen >> 8) & 0xff); 
    header[42] = (byte) ((totalAudioLen >> 16) & 0xff); 
    header[43] = (byte) ((totalAudioLen >> 24) & 0xff); 

    out.write(header, 0, 4088); 
} 

private View.OnClickListener btnClick = new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
     case R.id.btnStart: { 
      AppLog.logString("Start Recording"); 

      enableButtons(true); 
      startRecording(); 

      break; 
     } 
     case R.id.btnStop: { 
      AppLog.logString("Stop Recording"); 

      enableButtons(false); 
      stopRecording(); 

      break; 
     } 
     } 
    } 
}; 

public void SendAudio(final short data[]) { 
    Thread thrd = new Thread(new Runnable() { 
     String LOG_TAG = null; 
     long SAMPLE_INTERVAL = 10; 

     @Override 
     public void run() { 
      Log.e(LOG_TAG, "start send thread, thread id: "); 

      long file_size = 0; 
      int bytes_read = 0; 
      int bytes_count = 0; 

      // byte[] buf = new byte[bufferSize]; 
      // buf = data; 

      try { 
       // byte [] b = "192.168.1.40".getBytes(); 
       byte[] b = new byte[] { (byte) 192, (byte) 168, (byte) 1, 
         (byte) 39 }; 

       InetAddress addr = InetAddress.getLocalHost(); 
       InetAddress addr1 = InetAddress.getByAddress(b); 

       HttpClient httpclient = new DefaultHttpClient(); 

       ServerUrl url = new ServerUrl(); 

       String url_page = ServerUrl.url_audio; 
       // String value = new String(buf); 

       // System.out.println("mmdata--------- " + value); 

       HttpPost httppost = new HttpPost(url_page); 

       // Unix time stamp 
       long unixTime = System.currentTimeMillis()/1000L; 
       String timestamp = String.valueOf(unixTime); 

       // Json Format 
       JSONObject holder = new JSONObject(); 
       JSONArray jArray = new JSONArray(); 
       try { 
        holder.put("UserId", "1"); 

        holder.put("Timestamp", timestamp); 
        // holder.put("length", value.length()); 
        for (int i = 0; i < buffer.length; i++) { 
         jArray.put(i, buffer[i]); 
        } 

        holder.put("MMData", jArray); 
        System.out.println("ARRAYYYY" + jArray); 



       } catch (JSONException e1) { 
        e1.printStackTrace(); 
       } 

       try { 

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
          2); 

        nameValuePairs.add(new BasicNameValuePair("MMData", 
          jArray.toString())); 

        httppost.setEntity(new UrlEncodedFormEntity(
          nameValuePairs)); 

        StringEntity se = new StringEntity(jArray.toString()); 

        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, 
          "application/json")); 

        httppost.setEntity(se); 

        HttpResponse response = httpclient.execute(httppost); 

       } catch (ClientProtocolException e) { 
        System.out.println("-------13"); 
        e.printStackTrace(); 
       } catch (IOException e) { 
        System.out.println("-------14"); 
        e.printStackTrace(); 
       } catch (Exception e) { 
        System.out.println("-------15"); 
        System.out.println("Exception"); 
        e.printStackTrace(); 
       } 
       bytes_count += bytes_read; 
       Log.d(LOG_TAG, "bytes_count : " + bytes_count); 
       Thread.sleep(SAMPLE_INTERVAL, 0); 

      } catch (InterruptedException ie) { 
       Log.e(LOG_TAG, "InterruptedException"); 
      } catch (UnknownHostException uhe) { 
       Log.e(LOG_TAG, "UnknownHostException"); 
      } catch (IOException ie) { 
       Log.e(LOG_TAG, "IOException"); 
      } 
     } // end run 
    }); 
    thrd.start(); 
} 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 

    if (keyCode == KeyEvent.KEYCODE_BACK) { 

     finish(); 
    } 
    return super.onKeyDown(keyCode, event); 
} 
} 

帮助将不胜感激。我已经做了这个搜索,但力得到适当的解决方案..我想所有其他的可能性但没有成功。

非常感谢

+0

您能告诉我们如何调用WriteWaveFileHeader吗?即你传递给它的论据的价值是什么? – Michael 2012-08-08 14:01:27

+0

@Michael我已更新代码.. – 2012-08-09 04:13:20

+0

生成的文件大小与您预期的相同吗?如果您在音频编辑器(如Audacity)中打开原始数据,您应该能够通过尝试不同的导入设置找出采样率/#频道的真正含义,直到您正确播放它为止。 – Michael 2012-08-09 08:57:44

回答

6

我得到了解决。这里是我的代码

public class Audio_Record extends Activity { 
private static final int RECORDER_SAMPLERATE = 8000; 
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO; 
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT; 
private AudioRecord recorder = null; 
private Thread recordingThread = null; 
private boolean isRecording = false; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    setButtonHandlers(); 
    enableButtons(false); 

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

    System.out.println("BUFFER SIZE VALUE IS " + bufferSize); 

} 

private void setButtonHandlers() { 
    ((Button) findViewById(R.id.btnStart)).setOnClickListener(btnClick); 
    ((Button) findViewById(R.id.btnStop)).setOnClickListener(btnClick); 
} 

private void enableButton(int id, boolean isEnable) { 
    ((Button) findViewById(id)).setEnabled(isEnable); 
} 

private void enableButtons(boolean isRecording) { 
    enableButton(R.id.btnStart, !isRecording); 
    enableButton(R.id.btnStop, isRecording); 
} 

int BufferElements2Rec = 1024; // want to play 2048 (2K) since 2 bytes we 
           // use only 1024 
int BytesPerElement = 2; // 2 bytes in 16bit format 

private void startRecording() { 

    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(); 
} 

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 = "/sdcard/voice8K16bitmono.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; 
    } 
} 

private View.OnClickListener btnClick = new View.OnClickListener() { 
    public void onClick(View v) { 
     switch (v.getId()) { 
     case R.id.btnStart: { 
      enableButtons(true); 
      startRecording(); 
      break; 
     } 
     case R.id.btnStop: { 
      enableButtons(false); 
      stopRecording(); 
      break; 
     } 
     } 
    } 
}; 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 

    if (keyCode == KeyEvent.KEYCODE_BACK) { 

     finish(); 
    } 
    return super.onKeyDown(keyCode, event); 
} 
} 
+0

工作正常,无需使用wav标头。以pcm格式录制音频文件并播放。 – 2012-09-22 07:56:19

1

int channels = 1而不是int channels = 2CHANNELS = AudioFormat.CHANNEL_IN_MONO

4

我对自己实施这个很麻烦。我做了上面所有人都说过的很多事情。我实现了这个功能,因为它是自己的Recorder类(并且包含了所有上述“修复”以及其他一些调整)。我希望我的代码可以帮助其他人节省我花费在这个上的许多时间:

public class Recorder { 

    //Begin private fields for this class 
    private AudioRecord recorder; 

    private static final int RECORDER_BPP = 16; 
    private static final String AUDIO_RECORDER_FILE_EXT_WAV = "AudioRecorder.wav"; 
    private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder"; 
    private static final String AUDIO_RECORDER_TEMP_FILE = "record_temp.raw"; 
    private static final int RECORDER_SAMPLERATE = 8000; 
    private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO; 
    private static final int RECORDER_CHANNELS_INT = 1; 

    private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT; 

    private int bufferSize = 200000; 
    short[] buffer; 
    private Thread recordingThread = null; 
    private boolean isRecording = false; 

    //Constructor 
    Recorder(String uniquename) 
    { 
     //Initilize our recorder object 
     /*recorder = new AudioRecord(
       MediaRecorder.AudioSource.MIC, 
       44100, 
       AudioFormat.CHANNEL_IN_MONO, 
       AudioFormat.ENCODING_PCM_16BIT, 
       AudioRecord.getMinBufferSize(44100,AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT) 
       ); 
       */ 

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

      System.out.println("BUFFER SIZE VALUE IS " + bufferSize); 

      int buffercount = 4088/bufferSize; 
      if (buffercount < 1) 
       buffercount = 1; 
     recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, 
       RECORDER_SAMPLERATE, RECORDER_CHANNELS, 
       RECORDER_AUDIO_ENCODING, 44100); 

     //recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
     //recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 
     //recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); 


    } 

    public void start() throws IllegalStateException, IOException 
    { 

      buffer = new short[4088]; 

      recorder.startRecording(); 

      isRecording = true; 

      recordingThread = new Thread(new Runnable() 
      { 
       @Override 
       public void run() { 
        writeAudioDataToFile(); 
       } 
      }, "AudioRecorder Thread"); 

      recordingThread.start(); 
    } 

    public void stop() 
    { 
     System.out.println("Told to stop"); 
     stopRecording(); 
    } 

    public boolean isRecording() 
    { 
     if(recorder.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) 
      return true; 
     else 
      return false; 
    } 


    private void stopRecording() { 
     // stops the recording activity 

     if (null != recorder) { 
      isRecording = false; 

      recorder.stop(); 


      recorder.release(); 

       recorder = null; 
       recordingThread = null;} 
      // copy the recorded file to original copy & delete the recorded copy 
      copyWaveFile(getTempFilename(), getFilename()); 
      deleteTempFile(); 
    } // stores the file into the SDCARD 
     private String getFilename() { 
      System.out.println("---3---"); 
      String filepath = Environment.getExternalStorageDirectory().getPath(); 
      File file = new File(filepath, AUDIO_RECORDER_FOLDER); 

      if (!file.exists()) { 
       file.mkdirs(); 
      } 

      return (file.getAbsolutePath() + "/" + AUDIO_RECORDER_FILE_EXT_WAV); 
     } 


    private void deleteTempFile() { 
     File file = new File(getTempFilename()); 

     file.delete(); 
    } 


    private void copyWaveFile(String inFilename, String outFilename) { 
    System.out.println("---8---"); 
    FileInputStream in = null; 
    FileOutputStream out = null; 
    long totalAudioLen = 0; 
    long totalDataLen = totalAudioLen + 36; 
    long longSampleRate = RECORDER_SAMPLERATE; 
    int channels = RECORDER_CHANNELS_INT; 
    long byteRate = RECORDER_BPP * RECORDER_SAMPLERATE * channels/8; 

    byte[] data = new byte[bufferSize]; 

    try { 
     in = new FileInputStream(inFilename); 
     out = new FileOutputStream(outFilename); 
     totalAudioLen = in.getChannel().size(); 
     totalDataLen = totalAudioLen + 36; 

     Controller.doDoc("File size: " + totalDataLen, 4); 

     WriteWaveFileHeader(out, totalAudioLen, totalDataLen, 
       longSampleRate, channels, byteRate); 
     byte[] bytes2 = new byte[buffer.length * 2]; 
     ByteBuffer.wrap(bytes2).order(ByteOrder.LITTLE_ENDIAN) 
       .asShortBuffer().put(buffer); 
     while (in.read(bytes2) != -1) { 
      out.write(bytes2); 
     } 

     in.close(); 
     out.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

    // stores the file into the SDCARD 
    private String getTempFilename() { 
     // Creates the temp file to store buffer 
     System.out.println("---4-1--"); 
     String filepath = Environment.getExternalStorageDirectory().getPath(); 
     System.out.println("---4-2--"); 
     File file = new File(filepath, AUDIO_RECORDER_FOLDER); 
     System.out.println("---4-3--"); 

     if (!file.exists()) { 
      file.mkdirs(); 
     } 

     File tempFile = new File(filepath, AUDIO_RECORDER_TEMP_FILE); 
     System.out.println("---4-4--"); 

     if (tempFile.exists()) 
      tempFile.delete(); 
     System.out.println("---4-5--"); 
     return (file.getAbsolutePath() + "/" + AUDIO_RECORDER_TEMP_FILE); 
    } 
    private void writeAudioDataToFile() { 

     // Write the output audio in byte 
     byte data[] = new byte[bufferSize]; 

     String filename = getTempFilename(); 
     // 
     FileOutputStream os = null; 
     // 
     try { 
      // 
      os = new FileOutputStream(filename); 
      // 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 

     int read = 0; 


     // if (null != os) { 
     while (isRecording) { 
      // gets the voice output from microphone to byte format 
      recorder.read(buffer, 0, buffer.length);    
      // read = recorder.read(data, 0, 6144); 

      if (AudioRecord.ERROR_INVALID_OPERATION != read) { 
       try { 
        // // writes the data to file from buffer 
        // // stores the voice buffer 

        // short[] shorts = new short[bytes.length/2]; 
        // to turn bytes to shorts as either big endian or little 
        // endian. 
        // ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts); 

        // to turn shorts back to bytes. 
        byte[] bytes2 = new byte[buffer.length * 2]; 
        ByteBuffer.wrap(bytes2).order(ByteOrder.LITTLE_ENDIAN) 
          .asShortBuffer().put(buffer); 

        os.write(bytes2); 
       // ServerInteractor.SendAudio(buffer); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 

     try { 
      os.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    private void WriteWaveFileHeader(FileOutputStream out, long totalAudioLen, 
      long totalDataLen, long longSampleRate, int channels, long byteRate) 
      throws IOException { 
     System.out.println("---9---"); 
     byte[] header = new byte[4088]; 

     header[0] = 'R'; // RIFF/WAVE header 
     header[1] = 'I'; 
     header[2] = 'F'; 
     header[3] = 'F'; 
     header[4] = (byte) (totalDataLen & 0xff); 
     header[5] = (byte) ((totalDataLen >> 8) & 0xff); 
     header[6] = (byte) ((totalDataLen >> 16) & 0xff); 
     header[7] = (byte) ((totalDataLen >> 24) & 0xff); 
     header[8] = 'W'; 
     header[9] = 'A'; 
     header[10] = 'V'; 
     header[11] = 'E'; 
     header[12] = 'f'; // 'fmt ' chunk 
     header[13] = 'm'; 
     header[14] = 't'; 
     header[15] = ' '; 
     header[16] = 16; // 4 bytes: size of 'fmt ' chunk 
     header[17] = 0; 
     header[18] = 0; 
     header[19] = 0; 
     header[20] = 1; // format = 1 
     header[21] = 0; 
     header[22] = (byte) RECORDER_CHANNELS_INT; 
     header[23] = 0; 
     header[24] = (byte) (longSampleRate & 0xff); 
     header[25] = (byte) ((longSampleRate >> 8) & 0xff); 
     header[26] = (byte) ((longSampleRate >> 16) & 0xff); 
     header[27] = (byte) ((longSampleRate >> 24) & 0xff); 
     header[28] = (byte) (byteRate & 0xff); 
     header[29] = (byte) ((byteRate >> 8) & 0xff); 
     header[30] = (byte) ((byteRate >> 16) & 0xff); 
     header[31] = (byte) ((byteRate >> 24) & 0xff); 
     header[32] = (byte) (RECORDER_CHANNELS_INT * RECORDER_BPP/8); // block align 
     header[33] = 0; 
     header[34] = RECORDER_BPP; // bits per sample 
     header[35] = 0; 
     header[36] = 'd'; 
     header[37] = 'a'; 
     header[38] = 't'; 
     header[39] = 'a'; 
     header[40] = (byte) (totalAudioLen & 0xff); 
     header[41] = (byte) ((totalAudioLen >> 8) & 0xff); 
     header[42] = (byte) ((totalAudioLen >> 16) & 0xff); 
     header[43] = (byte) ((totalAudioLen >> 24) & 0xff); 

     out.write(header, 0, 4088); 
    } 
}