Android audio and video goes deep into an AudioRecord recording to generate pcm into wav (with source code download), androidaudiorecord

Source: Internet
Author: User

Android audio and video goes deep into an AudioRecord recording to generate pcm into wav (with source code download), androidaudiorecord

This project address is named AudioRecord recording (can be paused, convert pch to wav ).
Https://github.com/979451341/Audio-and-video-learning-materials

Here is an official description

1. AndioRecord Overview

The main function of the AndioRecord class is to allow various JAVA applications to manage audio resources so that they can use this class to record the sound collected by sound-related hardware. This function is implemented by reading the audio data of the AudioRecord object. In the recording process, you need to obtain the recording data of the AudioRecord object in a timely manner through one of the following three methods. the AudioRecord class provides three methods for obtaining sound data: read (byte [], int, int), read (short [], int, int), read (ByteBuffer, int ). no matter which method you choose to use, you must set the storage format for your audio data in advance.

When starting the recording, AudioRecord needs to initialize an associated sound buffer, which is mainly used to save New sound data. The buffer size, which can be specified during object construction. It indicates the amount of audio that can be recorded before an AudioRecord object is read (synchronized ). Audio Data is read from the audio hardware. The data size cannot exceed the size of the entire recording data (which can be read multiple times), that is, the data of the buffer capacity is read each time.

2. Create an AudioRecord object

AudioRecord (int audioSource, int sampleRateInHz, int channelConfig, int audioFormat, int bufferSizeInBytes)

AudioSource
Audio Source: Where to collect audio. Here we certainly collect audio from the microphone, so the value of this parameter is MIC

SampleRateInHz
Sampling Rate: the sampling frequency of the audio. The number of samples per second. The higher the sampling rate, the higher the sound quality. The instances provided are 44100, 22050, 11025, but are not limited to these parameters. For example, low sampling rates such as 4000 and 8000 can be used to collect low-quality audio.

ChannelConfig
Sound channel settings: android supports dual-channel stereo and single-channel. MONO single channel, STEREO

AudioFormat
Encoding standard and sampling size: Of course, the collected data uses PCM encoding (pulse code modulation encoding, that is, PCM encoding. PCM converts a continuously changing analog signal to a digital code by sampling, quantization, and encoding .) The sample size supported by android is 16-bit or 8-bit. Of course, the larger the sampling size, the more information, the higher the sound quality. Now the mainstream sampling size is 16 bit, 8 bit is enough for low-quality voice transmission.

BufferSizeInBytes
The size of the buffer required for data collection. If you do not know the minimum size, you can view it in getMinBufferSize.
AudioRecord. getMinBufferSize (sampleRateInHz,
ChannelConfig, channelConfig );


3. PCM and WAV Files

PCM

PCM is a common encoding format used to convert analog signals to digital signals. It is called pulse encoding modulation. PCM divides analog signals into multiple segments according to a certain distance, then, the intensity of each spacing is quantified by binary. PCM indicates the amplitude of a piece of audio in an audio file over time. Android supports PCM audio data in WAV Files.

WAV Files

WAV and MP3 are common audio formats. Different encoding formats use different encoding methods for the original audio, which are usually used for convenient transmission, the original audio is compressed. To enable the player to recognize this format, the header files in each format are specific and have certain rules, in this way, the player can recognize the format and then play the audio files following the corresponding decoding algorithm.


4. Code running process

First, create and configure AudioRecord

// Audio input-microphone private final static int AUDIO_INPUT = MediaRecorder. audioSource. MIC; // use frequency // 44100 is the current standard, but some devices still support, 11025 // the sampling frequency is generally divided into three levels: 22.05 KHz, 44.1 KHz, and 48 KHz: private final static int AUDIO_SAMPLE_RATE = 16000; // audio channel private final static int AUDIO_CHANNEL = AudioFormat. CHANNEL_IN_MONO; // code private final static int AUDIO_ENCODING = AudioFormat. ENCODING_PCM_16BIT; // buffer byte size private int bufferSizeInBytes = 0; // recording object private AudioRecord audioRecord; /*** create a default recording object ** @ param fileName file name */public void createdefaauaudio (String fileName) {// obtain the buffer byte size bufferSizeInBytes = AudioRecord. getMinBufferSize (AUDIO_SAMPLE_RATE, AUDIO_CHANNEL, AUDIO_ENCODING); audioRecord = new AudioRecord (AUDIO_INPUT, audio, AUDIO_CHANNEL, AUDIO_ENCODING, bufferSizeInBytes); this. fileName = fileName; status = Status. STATUS_READY ;}

Start recording, and open a sub-thread to put the recording data into the pcm file.

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

How to Write audio into files is important. I wrote a pseudocode to explain the running sequence of this Code.

First, create a pcm file to get its FileOutputStream, and then continuously loop AudioRecord to put the recording data into a byte array through read. When the recording ends, remember to stop this loop.

// 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 file = new File (currentFileName); if (file. exists () {file. delete ();} fos = new FileOutputStream (file); // create an accessable Byte file} catch (IllegalStateException e) {Log. e ("AudioRecorder", e. getMessage (); throw new IllegalStateException (e. getMessage ();} catch (FileNot FoundException 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 (audiodata);} catch (IOException e) {Log. e ("AudioRecorder", e. getMessage ());}}}

How can we pause the recording, As long as AudioRecord. stop on the line, and then when the recording continues in AudioRecord. start is fine, but we need to create another pcm record. When the recording ends, we need to convert these pcm together into a wav,

The code for converting pcm to wav is fixed, so I will not post it. You can choose the address of the first code in the article.

References
Http://blog.csdn.net/hellofeiya/article/details/8968534

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

 

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.