Android Audio Video In-depth one Audiorecord recording generated PCM converted to WAV (with source download)

Source: Internet
Author: User

This project address, name is Audiorecord recording (can pause, convert PCH to WAV), for star
Https://github.com/979451341/Audio-and-video-learning-materials

First, the official description.

1.AndioRecord Approximate description
The main function of the Andiorecord class is to enable various Java applications to manage audio resources so that they can record sound collected by sound-related hardware. The implementation of this feature is accomplished by "pulling" (reading) the Audiorecord object's sound data. In the recording process, the application needs to do is to obtain the Audiorecord object's recording data in a timely manner through one of the following three class methods. The Audiorecord class provides three ways to get sound data, namely read (byte[], int, int), read (short[], int, int), read (bytebuffer, int). Whichever method you choose to use, you must set up a storage format for user-friendly sound data beforehand.

To start recording, Audiorecord needs to initialize an associated sound buffer, which is primarily used to store new sound data. The size of this buffer, which we can specify during the construction of the object. It indicates how long a Audiorecord object can record (that is, the volume of sound that can be recorded at one time) before it is read (synchronized) by the sound data. Sound data is read from the audio hardware, and the data size does not exceed the size of the entire recording data (which can be read multiple times), which is the data that initializes the buffer capacity each time it is read.

2.AudioRecord Object creation
Audiorecord (int audiosource, int samplerateinhz, int channelconfig, int audioformat, int buffersizeinbytes)

Audiosource
Audio Source: Refers to where the audio is collected. Here we are, of course, collecting audio from the microphone, so the value of this parameter is mic

Samplerateinhz
Sample rate: Audio sampling frequency, the number of times per second can be sampled, the higher the sampling rate, the higher the sound quality. The examples given are 44100, 22050, 11025 but not limited to these parameters. For example, to collect low-quality audio, you can use a low sample rate of 4000, 8000, and so on.

Channelconfig
Channel settings: Android supports two-channel stereo and mono. Mono mono, stereo stereo

Audioformat
encoding format and sample size: The data collected is of course used PCM encoding (pulse code modulation encoding, PCM encoding. PCM converts a continuously varying analog signal to a digital encoding by sampling, quantization, and coding in three steps. Android supports a sample size of 16bit or 8bit. Of course, the larger the sample size, the more information, the higher the sound quality, now the mainstream sampling size is 16bit, in the low-quality voice transmission 8bit enough.

Buffersizeinbytes
The size of the buffer needed to collect the data, if not known, the minimum required size can be viewed in getminbuffersize ().
Audiorecord.getminbuffersize (Samplerateinhz,
Channelconfig, Channelconfig);

3.PCM and WAV files
Pcm

PCM is a commonly used encoding format for converting analog signals to digital signals, called pulse-coded modulation, in which PCM divides analog signals into multiple segments at a certain distance, and then quantifies the strength of each pitch by means of a binary binary. PCM represents the amplitude of a piece of audio in an audio file that passes over time. Android supports PCM audio data in WAV files.

WAV file

Wav,mp3 etc is our more common audio format, different encoding format for the original audio encoding method is also different, usually for the convenience of transmission and other problems, the original audio will be compressed, and in order to enable the player to recognize the format, so in each format of the header file is specific, There are certain rules to let the player recognize the format, and then follow the corresponding decoding algorithm to play back audio files.

4. Code Run Process

The first is to create and configure Audiorecord

Audio Input-Microphone
Private final static int audio_input = MediaRecorder.AudioSource.MIC;
Adoption Frequency
44100 is the current standard, but some devices still support 22050,16000,11025
Sampling frequency is generally divided into 22.05KHz, 44.1KHz, 48KHz three levels
Private final static int audio_sample_rate = 16000;
Channel Mono Channel
Private final static int audio_channel = Audioformat.channel_in_mono;
Coding
Private final static int audio_encoding = Audioformat.encoding_pcm_16bit;
Buffer byte size
private int buffersizeinbytes = 0;

//录音对象private AudioRecord audioRecord;/** * 创建默认的录音对象 * * @param fileName 文件名 */public void createDefaultAudio(String fileName) {    // 获得缓冲区字节大小    bufferSizeInBytes = AudioRecord.getMinBufferSize(AUDIO_SAMPLE_RATE,            AUDIO_CHANNEL, AUDIO_ENCODING);    audioRecord = new AudioRecord(AUDIO_INPUT, AUDIO_SAMPLE_RATE, AUDIO_CHANNEL, AUDIO_ENCODING, bufferSizeInBytes);    this.fileName = fileName;    status = Status.STATUS_READY;}

Start recording while opening a sub-thread to put the recorded data into the PCM file

    audioRecord.startRecording();    new Thread(new Runnable() {        @Override        public void run() {            writeDataTOFile(listener);        }    }).start();

How to write audio to a file is the point, I write a pseudo-code that shows the order of the Code run

First create the PCM file, get his fileoutputstream, and then loop Audiorecord through read to put the recorded data into a byte array, when the recording ends remember to stop this cycle

 //New A byte array to store some byte data, size of buffer size byte[] Audiodata = new Byte[buffersizeinbytes];    FileOutputStream fos = null;    int readsize = 0;        try {File File = new file (currentfilename);        if (file.exists ()) {file.delete (); The FOS = new FileOutputStream (file);//Create an accessible byte of files} catch (IllegalStateException e) {log.e ("Audiorecord        Er ", e.getmessage ());    throw new IllegalStateException (E.getmessage ());    } catch (FileNotFoundException e) {log.e ("Audiorecorder", E.getmessage ());        } while (true) {readsize = Audiorecord.read (audiodata, 0, buffersizeinbytes); if (audiorecord.error_invalid_operation! = readsize && fos! = null) {try {fos.write (AU            Diodata);            } catch (IOException e) {log.e ("Audiorecorder", E.getmessage ()); }        }    }

How we want to implement the ability to pause the recording, as long as the audiorecord.stop on the line, and then when the recording continues in the Audiorecord.start, but to create another PCM record, when the recording is over we will convert the PCM together into a WAV,

As for PCM conversion to WAV code is fixed, I do not post, everyone in the article first code address pick up

Reference articles
http://blog.csdn.net/hellofeiya/article/details/8968534

http://blog.csdn.net/JenseaChen/article/details/46883319

Android Audio Video In-depth one Audiorecord recording generated PCM converted to WAV (with source download)

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.