I want to do some Audio signal processing in the platform of Android. So I use AudioTrack record sounds clip with 10s. However, when I check the record data file, I found there is a bunch of zeros in the very beginning in every time. Can someone tell me why it like that. And how to eliminate those zeros. Thank you!
Here is my code: 1. Initialize a new file for data stroage:
file = new File(Environment.getExternalStorageDirectory(), "RecordFile"); if (file.exists()) file.delete(); try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); }
2. Paras:
private static final int Encording = AudioFormat.ENCODING_PCM_16BIT; public static final int Sample_rate = 48000; private static final int Channel = AudioFormat.CHANNEL_IN_MONO; public static final int Buffersize = AudioRecord.getMinBufferSize(Sample_rate, Channel, Encording);
-
Record
private void AudioRecord() { try { AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, Sample_rate, Channel, Encording, Buffersize); DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(MainActivity.file))); short[] buffer = new short[Buffersize / 2]; audioRecord.startRecording(); // Start record /*******************************************************************************************************/ while (MainActivity.isrecord) { audioRecord.read(buffer, 0, buffer.length); for (int i = 0; i < buffer.length; i++) { dos.writeShort(buffer[i]); } } audioRecord.stop(); audioRecord.release(); dos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
}