Note that there are some explanatory texts on larger screens.

plurals
  1. POAudioRecord - writing PCM file
    primarykey
    data
    text
    <p>I' m trying to record some voice using AudioRecord class and then write it to output .pcm file. I want my program to keep recording until the stop button is pressed. Unfortunatelly no matter how long I' m recording, output file size is always 3528 bytes and it lasts for about 20 ms. Also according to Toolsoft Audio Tools, properies of that file are: 44100Hz, 16 bits, stereo, even if I' m using mono with completely different sampling frequency.</p> <pre><code>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; } } </code></pre> <p>May I kindly ask you to tell me what's wrong? I'm hoping the answer won't be 'everything' :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload