Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. public void startRecording(){
  2.  
  3. int recordingBufferSize = AudioRecord.getMinBufferSize(AUDIO_SAMPLE_FREQUENCY_Hz, AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT) * 4;
  4.  
  5. if (mAudioFile.exists())// Delete any previous recording.
  6. mAudioFile.delete();
  7.  
  8. try {
  9. mAudioFile.createNewFile();
  10. } catch (IOException e) {
  11. throw new IllegalStateException("Failed to create " + mAudioFile.toString());
  12. }
  13.  
  14. try {
  15.  
  16. // Create a new AudioRecord object to record the audio.
  17. audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
  18. AUDIO_SAMPLE_FREQUENCY_Hz, AudioFormat.CHANNEL_CONFIGURATION_STEREO,
  19. AudioFormat.ENCODING_PCM_16BIT, recordingBufferSize);
  20.  
  21. audioRecord.startRecording();
  22.  
  23. FileOutputStream fos = new FileOutputStream(mAudioFile);
  24. FileChannel fchannel = fos.getChannel();
  25.  
  26. ByteBuffer recordingBuffer = ByteBuffer.allocateDirect(recordingBufferSize);
  27. recordingBuffer.order(ByteOrder.LITTLE_ENDIAN);
  28.  
  29. //Log.w("SoundTouchJNI", "Native Byte Order is "+ sampleBuffer.order());
  30. while (isRecording) {
  31.  
  32. int bufferReadResult = audioRecord.read(recordingBuffer, recordingBufferSize);
  33.  
  34. Log.w("SoundTouchJNI", "Writing bytes "+ String.valueOf(bufferReadResult));
  35. fchannel.write(recordingBuffer);
  36. recordingBuffer.rewind();
  37. }
  38. fchannel.close();
  39.  
  40. audioRecord.stop();
  41. fos.close();
  42.  
  43.  
  44. }catch (Throwable t) {
  45. Log.e("AudioRecord","---------------- " + t.getMessage());
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement