Android Microphone Audio sampling and use

Source: Internet
Author: User

Android Phone Microphone Sampling Some code is found on the Internet, specifically do not remember.

Use the sample result and the function code of your own app:

1. In some apps, the audio input of the microphone will need to be used, such as a very hot blown skirt;

2. Sampling Thread:

public class Microlevelrunnable implements Runnable {
Private static final String tag = "Microlevelthread";
Private Audiorecord ar;
Private short BS;
One 025 Hz, 8 bit sound called the telephone quality;
050 Hz, the voice of the bit is called broadcast sound quality;
[4] The * * * has reached the CD's sound quality.
Private final int sample_rate_in_hz = 22050;
Private Boolean isrun = false;

public static final int msg_v = 0;
public static final int msg_vv = 1;
public static final int msg_vf = 2;
public static final int msg_vo = 3;

public static final int page_need_water = 10;

public static int bubble_speed = 2;//is called every n times, out of a bubble

Public microlevelrunnable () {
Super ();
}

public void Stop () {
Isrun = false;
Ar.stop ();
Ar.release ();
AR = null;
}

public void Run () {
BS = (short) audiorecord.getminbuffersize (Sample_rate_in_hz,
Audioformat.channel_configuration_mono,
Audioformat.encoding_pcm_16bit);
AR = new Audiorecord (MediaRecorder.AudioSource.MIC, Sample_rate_in_hz,
Audioformat.channel_configuration_mono,
Audioformat.encoding_pcm_16bit, BS);
Used to read the
Ar.startrecording ();
short[] buffer = new SHORT[BS];
Isrun = true;
int speed = 1;
while (true) {
if (!isrun) {
try {
Synchronized (this) {
LOG.V (tag, "wait..");
This.wait ();
}
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
speed++;
int r = ar.read (buffer, 0, BS);
int v = 0;
for (int i = 0; i < buffer.length; i++) {
V + = buffer[i] * Buffer[i];
}
int value = (int) (Math.Abs (int) (V/(float) r)/10000) >> 1);

Double DB = ten * MATH.LOG10 (Math.Abs (v)/(double) r);

Message msg = new Message ();
Msg.what = Msg_v;
MSG.ARG1 = value;
MSG.ARG2 = (int) DB;
// -------------------------------------------
Use the following method to distinguish whether it is blowing, the disadvantage is that you can simulate
DB White noise below 50 for noise, greater than 60 for invalid
Value less than 38 is silent, greater than 60 no invalid

if (Speed > Bubble_speed) {
BubbleMain.MainHandle.sendMessage (msg);
Speed = 1;
}
}
}

}

3. Start the sampling thread:

mrunnable = new Microlevelrunnable ();
Mthread = new Thread (mrunnable);
Mthread.start ();

4. My Android App "http://openbox.mobilem.360.cn/index/d/sid/341284 Http://zhushou.360.cn/detail/index/soft_id/ 341284 "Blowing bubble in the use of the above function module

The application is a typical application of the Web+android component, the presentation of blowing bubbles is to optimize the modified Web program, through the WebView interface and the combination of the microphone sampling thread, to achieve the effect of blowing bubbles on the phone:

Android Microphone Audio sampling and use

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.