"Android Development" for real-time voice data capture/Playback

Source: Internet
Author: User

Recently done project is and voice real-time capture and send, the other side real-time receive and play related, the following record the implementation of the core code.
Many Android developers should know that Android has a Mediarecorder object and a MediaPlayer object for recording and playing audio. The disadvantage is that they cannot be collected in real time and sent out, so we can only use Audiorecord and audiotrack to achieve.
Remember to assert your authority:

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /><uses-permission android:name="android.permission.RECORD_AUDIO" >

First, Audiorecord implementation of the core code is described as follows:
1, first declare the relevant recording configuration parameters

private AudioRecord audioRecord;// 录音对象privateint8000;// 采样率 8000privateint channelInConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;// 定义采样通道privateint audioEncoding = AudioFormat.ENCODING_PCM_16BIT;// 定义音频编码(16位)privatebytenull;// 录制的缓冲数组

2. Before we start recording, we need to initialize the Audiorecord class.

// 根据定义好的几个配置,来获取合适的缓冲大小// int bufferSize = 800;int bufferSize = AudioRecord.getMinBufferSize(frequence,        channelInConfig, audioEncoding);// 实例化AudioRecordnew AudioRecord(MediaRecorder.AudioSource.MIC,        frequence, channelInConfig, audioEncoding, bufferSize);// 定义缓冲数组newbyte[bufferSize];

3. Prepare to start recording, and continuously read the data using the loop.

audioRecord.startRecording();// 开始录制true;// 设置录制标记为true// 开始录制while (isRecording) {// 录制的内容放置到了buffer中,result代表存储长度int0, buffer.length);/*.....result为buffer中录制数据的长度(貌似基本上都是640)。剩下就是处理buffer了,是发送出去还是直接播放,这个随便你。*/}//录制循环结束后,记得关闭录制!!ifnull) {    audioRecord.stop();}

Second, the Audiotrack code implementation is described as follows:
1, the statement plays the relevant configuration.

privatenull;// 录音文件播放对象privateint8000;// 采样率 8000privateint channelInConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;// 定义采样通道privateint audioEncoding = AudioFormat.ENCODING_PCM_16BIT;// 定义音频编码(16位)privateint bufferSize = -1;// 播放缓冲大小

2. Initialize the Audiotrack object (initialized once, this object can be reused)

// 获取缓冲 大小bufferSize = AudioTrack.getMinBufferSize(frequence, channelInConfig,        audioEncoding);// 实例AudioTracknew AudioTrack(AudioManager.STREAM_MUSIC, frequence,        channelInConfig, audioEncoding, bufferSize,        AudioTrack.MODE_STREAM);

3. Use Audiotrack to play voice data.

//将语音数据写入即可。track.write(dataArray, buffer, len);

Question one:
As the current project is real-time acquisition, real-time delivery, so it is necessary to consider the size of the package, tested, we use 160 byte as a packet delivery can achieve a better playback effect (that is, a buffer is split into four send). The processing code is as follows:

//callback data through the Listener interfaceif(Audiorecordingcallback! = null) {intOffset = result% Max_data_length >0?1:0;//Split a buffer into several small packets max_data_length the maximum number of bytes for the packet     for(inti =0; I < result/max_data_length + offset; i++) {int length= Max_data_length;if((i +1) * max_data_length > result) {length= Result-i * MAX_DATA_LENGTH; }//write to callback interfaceaudiorecordingcallback.onrecording (buffer, I * max_data_length,length); }}

Question two:
Sometimes transmitted to play the sound will be a card, in order to solve such a problem, the temporary use of the dual-buffering voice mechanism to solve the problem optimization is obvious. The code is as follows:

"Android Development" for real-time voice data capture/Playback

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.