Android uses audiorecord recording-related and audio file Encapsulation

Source: Internet
Author: User

Mediarecord can be used for recording in Android, which is easy to operate. However, it is not professional enough to Process audio. If you want to process audio in real time or encapsulate the audio

You can use audiorecord for recording.

Here is a piece of code. Audio Recording in audiorecord and audio encapsulation in WAV format are implemented.

Audiotrack and audiotrack can be used for edge recording and play, you can refer to: http://blog.sina.com.cn/s/blog_6309e1ed0100j1rw.html

The code here is not played. But there are encapsulation and details, as follows:

Package COM. ppmeet; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import android. app. activity; import android. graphics. pixelformat; import android. media. audioformat; import android. media. audiorecord; import android. media. mediarecorder; import android. OS. bundle; import android. view. view; import android. view. View. onclicklistener; import android. view. window; import android. view. windowmanager; import android. widget. button;/*** Class Name: testaudiorecord <br> * class description: Use audiorecord for recording <br> * PS: <br> ** @ Version 1.00 2011/09/21 * @ author codyy) peijiangping */public class testaudiorecord extends activity {// audio retrieval source private int audiosource = mediarecorder. audiosource. mic; // sets the audio sampling rate. 44100 is the current standard, but some settings The backup still supports 11025, 44100, Private Static int samplerateinhz =; // set the audio recording channel channel_in_stereo to dual-channel, and channel_configuration_mono to single-channel Private Static int channelconfig = audioformat. channel_in_stereo; // Audio Data Format: PCM 16-bit each sample. Ensure device support. PCM 8-bit each sample. Not necessarily supported by devices. Private Static int audioformat = audioformat. encoding_pcm_16bit; // buffer byte size private int buffersizeinbytes = 0; private button start; private button stop; private audiorecord; private Boolean isrecord = false; // set the recording status. // Audioname specifies the Private Static final string Audioname = "/sdcard/love. raw "; // newaudioname Private Static final string newaudioname ="/sdcard/new.wav "; Public void onc Reate (bundle savedinstancestate) {super. oncreate (savedinstancestate); getwindow (). setformat (pixelformat. translucent); // enables the requestwindowfeature (window. feature_no_title); // remove the interface title getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen, windowmanager. layoutparams. flag_fullscreen); // reset the page size setcontentview (R. layout. main); Init ();} private void Init () {start = (button) This. findviewbyid (R. id. sta RT); stop = (button) This. findviewbyid (R. id. stop); start. setonclicklistener (New testaudiolistener (); stop. setonclicklistener (New testaudiolistener (); creataudiorecord ();} private void creataudiorecord () {// obtain the buffer byte size buffersizeinbytes = audiorecord. getminbuffersize (samplerateinhz, channelconfig, audioformat); // create the audiorecord object audiorecord = new audiorecord (audiosource, samplerateinhz, channelconfig, Audi Oformat, buffersizeinbytes);} class testaudiolistener implements onclicklistener {@ overridepublic void onclick (view v) {If (V = Start) {startrecord ();} If (V = stop) {stoprecord () ;}} private void startrecord () {audiorecord. startrecording (); // set the recording status to trueisrecord = true; // enable the audio file writing thread new thread (New audiorecordthread ()). start ();} private void stoprecord () {close ();} private void close () {If (audiorecord! = NULL) {system. out. println ("stoprecord"); isrecord = false; // stop writing files to audiorecord. stop (); audiorecord. release (); // release resource audiorecord = NULL;} class audiorecordthread implements runnable {@ overridepublic void run () {writedatetofile (); // write the raw data copywavefile (Audioname, newaudioname) to the file; // Add the header file to the raw data}/*** write the data to the file, but it cannot be played, because the audio obtained by audiorecord is RAW Raw raw audio, * if you need to play the audio, you must add some format or encoding header information. However, the advantage is that you can process the raw audio data. For example, you need to make a talking Tom * Cat to process the audio here, then re-encapsulate the audio. Therefore, it is easier to process the audio. */Private void writedatetofile () {// a new byte array is used to store some bytes of data. The size is the buffer size byte [] audiodata = new byte [buffersizeinbytes]; fileoutputstream Fos = NULL; int readsize = 0; try {file = new file (Audioname); If (file. exists () {file. delete ();} Fos = new fileoutputstream (File); // create an accessable Byte File} catch (exception e) {e. printstacktrace ();} while (isrecord = true) {readsize = audiorecord. read (audiodata, 0, buffersize Inbytes); If (audiorecord. error_invalid_operation! = Readsize) {try {FOS. write (audiodata);} catch (ioexception e) {e. printstacktrace () ;}}try {FOS. close (); // close the write stream} catch (ioexception e) {e. printstacktrace () ;}// obtain the private void copywavefile (string infilename, string outfilename) of the audio file that can be played. {fileinputstream in = NULL; fileoutputstream out = NULL; long totalaudiolen = 0; long totaldatalen = totalaudiolen + 36; long longsamplerate = samplerateinhz; int Chan Nels = 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 ();} catch (ioexception e) {e. printstacktrace () ;}}/*** a header is provided here. Insert this information to obtain the files that can be played. * This Is Why I inserted these 44 bytes. I haven't studied it in depth. However, if you open a WAV * audio file, you can find that the header file is basically the same. Files in each format have * their own header files. */Private void writewavefileheader (fileoutputstream out, long totalaudiolen, long totaldatalen, long longsamplerate, int channels, long byterate) throws ioexception {byte [] header = new byte [44]; header [0] = 'R'; // riff/wave headerheader [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] = 'W'; header [9] = 'a'; header [10] = 'V'; header [11] = 'E '; header [12] = 'F'; // 'fmt' chunkheader [13] = 'M'; header [14] = 'T'; header [15] = ''; header [16] = 16; // 4 Bytes: size of 'fmt' chunkheader [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 alignheader [33] = 0; header [34] = 16; // bits per sampleheader [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) ;}@ overrideprotected void ondestroy () {close (); super. ondestroy ();}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.