We do not know Editor's Note headers, but still got an MP3 file, sound quality is also very clear .AudioRecord is saved first recorded sound byte stream files, you can save the side edge reading, the header file is saved byte stream format audio file parsing.
public class MainActivity the extends AppCompatActivity { // audio acquisition sources Private int audioSource = MediaRecorder.AudioSource.MIC; // set the audio sampling rate, 44100 is the current standard, but some devices still support 22050,16000,11025 Private static int sampleRateInHz = 44100 ; // set channels have been recorded two-channel audio CHANNEL_IN_STEREO, CHANNEL_CONFIGURATION_MONO mono Private static int channelConfig = AudioFormat.CHANNEL_IN_STEREO; // audio data formats: PCM 16 bits per sample. To ensure that the device supports. PCM 8 bits per sample. Not be able to get the device supports. Private static int AudioFormat =AudioFormat.ENCODING_PCM_16BIT; // buffer size in bytes of private int bufferSizeInBytes = 0 ; private the Button the Start; private the Button the Stop; private AudioRecord AudioRecord; private Boolean isRecord = to false ; // set the state of being recorded // AudioName bare audio data file private static Final String AudioName = "/sdcard/AAAData/love.raw"; // not recommended so written, you can use Enviroment. // audio files that can be played NewAudioName Private static Final String NewAudioName = "/sdcard/AAAData/new.wav"; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFormat(PixelFormat.TRANSLUCENT);// 让界面横屏 requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉界面标题 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // 重新设置界面大小 setContentView(R.layout.activity_main); init(); } private void init() { Start = (Button) the this .findViewById (R.id.button); the Stop = (the Button) the this .findViewById (R.id.button2); Start.setOnClickListener ( new new TestAudioListener ()); Stop.setOnClickListener ( new new TestAudioListener ()); creatAudioRecord () ; } Private void creatAudioRecord () { // get buffer size in bytes bufferSizeInBytes = AudioRecord.getMinBufferSize (sampleRateInHz, channelConfig, AudioFormat); // Create Object AudioRecord AudioRecord = new new AudioRecord (audioSource, sampleRateInHz, channelConfig, audioFormat, bufferSizeInBytes); } class TestAudioListener implements View.OnClickListener { @Override public void onClick(View v) { if (v == Start) { startRecord(); } if (v == Stop) { stopRecord(); } } } private void startRecord() { audioRecord.startRecording(); // 让录制状态为true isRecord = true; //Thread on the audio file writing new new the Thread ( new new AudioRecordThread ()) Start ();. } Private void stopRecord () { Close (); } Private void Close () { IF (! AudioRecord = null ) { System.out.println ( "stopRecord Close" ); isRecord = to false ; // stop file write audioRecord.stop (); audioRecord.release (); // release resources AudioRecord = null ; } } classAudioRecordThread the implements the Runnable { @Override public void RUN () { writeDateTOFile (); // write data to the file bare copyWaveFile (AudioName, NewAudioName); // to bare data plus header files } } / ** * This data write in the file, but it did not play because of the original audio AudioRecord get naked audio * If you want to play it is necessary to add some format or coding of header information. But this advantage is that you can process the bare audio data, such as you would with a talkative TOM * cat in here for audio processing, and then re-packaged so that the audio obtained in this way is easier to do some audio processing . * / Private void writeDateTOFile () { // new new byte array is used to deposit a number of bytes of data, the size of the buffer size byte [] = audiodata new new byte[bufferSizeInBytes]; a FileOutputStream fos = null ; int value of readsize = 0 ; the try { File File = new new File (AudioName); IF (File.Exists ()) { File.delete (); } fos = new new a FileOutputStream (File); / / build a file accessible byte } the catch (Exception E) { e.printStackTrace (); } the while (== isRecord to true ) { value of readsizeAudioRecord.read = (audiodata, 0 , bufferSizeInBytes); IF (AudioRecord.ERROR_INVALID_OPERATION =! Value of readsize) { the try { fos.write (audiodata); } the catch (IOException E) { e.printStackTrace (); } } } the try { fos .close (); // Close for write } the catch (IOException E) { e.printStackTrace (); } } // obtained here can play audio files Private void copyWaveFile(String inFilename, String outFilename) { FileInputStream in = null; FileOutputStream out = null; long totalAudioLen = 0; long totalDataLen = totalAudioLen + 36; long longSampleRate = sampleRateInHz; int channels = 2; long byteRate = 16 * sampleRateInHz * channels / 8; byte[] data = new byte[bufferSizeInBytes]; try { in = new FileInputStream(inFilename); out = new FileOutputStream(outFilename); totalAudioLen = in.getChannel().size(); totalDataLen = totalAudioLen + 36; WriteWaveFileHeader(out, totalAudioLen, totalDataLen, longSampleRate, channels, byteRate); while (in.read(data) != -1) { out.write(data); } in.close(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } the catch (IOException E) { e.printStackTrace (); } } / ** * there is provided a header. Insert these information can be obtained files can be played. * Why insert this for my 44 bytes, this really is not in-depth study, but you just open a WAV * Audio files can be found in front of the header files can say basically the same, oh. Each file format has its * own unique headers. * / Private void WriteWaveFileHeader (a FileOutputStream OUT, Long totalAudioLen, Long totalDataLen, Long longSampleRate, int channels, Long byteRate) throws IOException { byte [] header = new new byte[44]; 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] = 'IN'; 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] = 16; // 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, 44); } @Override protected void onDestroy() { close(); super.onDestroy(); } }
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
To open the phone external storage rights