Android gets audiorecord microphone volume size and makes selective send

Source: Internet
Author: User

Extends:http://blog.csdn.net/alvinhuai/article/details/8955127,http://mikespook.com/2010/11/android-%e5%ae%9e% E6%97%b6%e8%8e%b7%e5%8f%96%e9%ba%a6%e5%85%8b%e9%a3%8e%e8%be%93%e5%85%a5%e9%9f%b3%e9%87%8f%e7%9a%84%e4%bb%a3%e7 %a0%81/

A few days ago to do a recording and get the volume size of the module, today write a demo and share. If you have a better way to remind me of a message, thank you.

First, the recording function is easy to achieve, through the Audiorecord or mediarecorder can be achieved, if you want to get the recording volume, with Audiorecord more convenient. The recording function can be broadly divided into several steps. An initial recording device Audiorecord. Second, turn on a thread to realize the recording function. Three gets the audio stream of the recording to analyze its size. Four passes the size to the main thread to make the UI change accordingly.

First initialize the Audiorecord. Audiorecord (int audiosource, int samplerateinhz, int channelconfig, int audioformat, int buffersizeinbytes), initialization requires five parameters, a  Udiosource refers to the recording source where we select the microphone: MediaRecorder.AudioSource.MIC.  Samplerateinhz The default sampling frequency, in Hertz, the official document says 44100 is compatible with all current devices, but if you test with a simulator, there are problems, so some 8000. Channelconfig, describes the audio channel settings Channel_in_mono guaranteed to work on all devices.  Audioformat Audio stream format, divided into 16bit or 8bit is currently supported by Encoding_pcm_16bit. Buffersizeinbytes The total number of audio data written to the buffer during the recording process (bytes). New audio data read from the buffer is always smaller than this value. This value is generally obtained by getminbuffersize. The parameters of the getminbuffersize can be referenced by the Audiorecord constructor. Execute the code in OnCreate.

try {
Mminibuffer = Audiorecord.getminbuffersize (Samplerates,
Audioformat.channel_in_mono,
Audioformat.encoding_pcm_16bit);
if (mminibuffer! = audiorecord.error_bad_value) {
Mrecord = new Audiorecord (MediaRecorder.AudioSource.MIC,
Samplerates[i],
Audioformat.channel_in_mono,
Audioformat.encoding_pcm_16bit,
Mminibuffer);
}
} catch (IllegalArgumentException e) {
;
}

This is the initialization of Audiorecord, the following can be achieved recording

public class Recordthread extends thread{
Private Boolean misrun = false;
Public Recordthread () {
Super ();
}
public void Run () {
Super.run ();
MainActivity.this.mRecord.startRecording ();
byte[] Byte_buffer = new Byte[mminibuffer];
Misrun = true;
while (Misrun) {
int r = mrecord.read (Byte_buffer,0,mminibuffer);
int mshortarraylenght = R/2;
short[] Short_buffer = new Short[mshortarraylenght];
Short_buffer = Bytearraytoshortarray (byte_buffer,mshortarraylenght);
int max = 0;
if (R > 0) {
for (int i=0; i<mshortarraylenght; i++) {
if (Math.Abs (Short_buffer[i]) > Max) {
max = Math.Abs (Short_buffer[i]);
}
}
Bundle Mbundle = new bundle ();
Mbundle.putint (Msenddata, Max);
Message MSG = new Message ();
Msg.what = recordstate;
Msg.setdata (Mbundle);
Mhandler.sendmessage (MSG);
}
}
MainActivity.this.mRecord.stop ();
Mhandler.sendemptymessage (Nullbuffer);
}

public void Stoprecord () {
Misrun = false;
}
}

Here is write a thread to implement the recording function. Byte_buffer Save the recorded audio stream, because there are a lot of recordings, I temporarily use the maximum value of each recording as the volume of the recording, and then return the maximum value to the main thread by handler. If you need to stop this thread you can call this thread function Stoprecord (); We can then use the OnDraw function of the view to draw the volume changes dynamically by the volume value we get each time. The specific code is not all put out, the main talk about this idea. If someone needs to be able to leave a message to me directly, can send to everybody.

Android gets audiorecord microphone volume size and makes selective send

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.