I am trying to record some voice using the AudioRecord class and then write it to the output .pcm file. I want my program to continue recording until the stop button is pressed. Unfortunately, no matter how much time I write, the size of the output file is always 3528 bytes, and it lasts about 20 ms. In addition, according to the Toolsoft Audio Tools, the properties of this file are: 44100 Hz, 16 bit, stereo, even if I use mono with a completely different sampling rate.
Thread recordingThread; boolean isRecording = false; int audioSource = AudioSource.MIC; int sampleRateInHz = 44100; int channelConfig = AudioFormat.CHANNEL_IN_MONO; int audioFormat = AudioFormat.ENCODING_PCM_16BIT; int bufferSizeInBytes = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat); byte Data[] = new byte[bufferSizeInBytes]; AudioRecord audioRecorder = new AudioRecord(audioSource, sampleRateInHz, channelConfig, audioFormat, bufferSizeInBytes); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } public void startRecording(View arg0) { audioRecorder.startRecording(); isRecording = true; recordingThread = new Thread(new Runnable() { public void run() { String filepath = Environment.getExternalStorageDirectory().getPath(); FileOutputStream os = null; try { os = new FileOutputStream(filepath+"/record.pcm"); } catch (FileNotFoundException e) { e.printStackTrace(); } while(isRecording) { audioRecorder.read(Data, 0, Data.length); try { os.write(Data, 0, bufferSizeInBytes); } catch (IOException e) { e.printStackTrace(); } try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } }); recordingThread.start(); } public void stopRecording(View arg0) { if (null != audioRecorder) { isRecording = false; audioRecorder.stop(); audioRecorder.release(); audioRecorder = null; recordingThread = null; } }
May I ask you to tell me what happened? I hope that the answer will not be "all" :)
Arxas source share