In the previous blog, we shared with you the national standard EasyGBD video push stream component in the development process, to achieve the video collection function, so the same, audio collection is also where we need to improve.
EasyGBD audio acquisition is based on AudioRecord implementation.
1. Permission application
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
Copy the code
2. Initialize
/* * set parameters to initialize AudioRecord constructor * audioSource: Input sources for audio collection, DEFAULT (DEFAULT), VOICE_RECOGNITION (for speech recognition, equivalent to DEFAULT), MIC (input by mobile phone microphone), * sampleRateInHz: sampling rate, note that currently 44.1khz is the only sampling rate guaranteed to be compatible with all Android phones * channelConfig: CHANNEL_IN_MONO (single channel), CHANNEL_IN_STEREO (double channel) * audioFormat: ENCODING_PCM_16BIT (16bit), ENCODING_PCM_8BIT (8bit) * bufferSizeInBytes: Configure the size of the audio buffer inside the AudioRecord, The value of the buffer cannot under a Frame "audio Frame (Frame) the size of the" * * / mAudioRecord = new AudioRecord (MediaRecorder. AudioSource. MIC, samplingRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);Copy the code
3. Start collecting
mAudioRecord.startRecording();
Copy the code
4. Read the collected sound data
/* * Continuously read the collected sound data, InputBuffers into the encoder * audioBuffer the buffer that stores data written to the audio recording * sizeInBytes the maximum number of bytes requested * public int read (ByteBuffer audioBuffer, int sizeInBytes) * */ len = mAudioRecord.read(inputBuffers[bufferIndex], BUFFER_SIZE);Copy the code
5. Stop collection and release resources
if (mAudioRecord ! = null) { mAudioRecord.stop(); mAudioRecord.release(); mAudioRecord = null; }Copy the code