Android microphone audio sampling and usage

Source: Internet
Author: User

Android microphone audio sampling and usage

Some codes of Android phone microphone sampling are found online.

Sample results and functional code of your app:

1. In some apps, You need to obtain and use the audio input of the microphone, for example, a hot skirt;

2. Sampling thread:

Public class MicroLevelRunnable implements Runnable {
Private static final String tag = "MicroLevelThread ";
Private AudioRecord ar;
Private short bs;
// 11 025Hz, 8 bit sound is called telephone sound quality;
// 22 050Hz and 16 bit sound are called Broadcast sound quality;
// 44 100Hz and 16 bit have achieved CD sound quality [4].
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; // every N calls, a bubble is generated

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 );
//
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 = 10 * 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 identify whether it is blow. The disadvantage is that it can be simulated.
// If the DB white noise is less than 50, the noise is greater than 60.
// If the value is less than 38, it is silent. If the value is greater than 60, no error is returned.

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 application [http://openbox.mobilem.360.cn/index/d/sid/341284 http://zhushou.360.cn/detail/index/soft_id/341284] blowing bubbles on the use of the above functional modules

This application is a typical application of web + android components. The presentation of blowing bubbles is a web program optimized and modified. Through the webview interface and the combination of microphone sampling threads, to achieve the effect of blowing bubbles on mobile phones:

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.