Android-improvement Article 10-audiorecord-Implementing "Hearing Aid"

Source: Internet
Author: User

This article from http://blog.csdn.net/hellogv/, reference must indicate the source!

Android can use mediarecorder and audiorecord to implement recording. mediarecorder directly stores the microphone data to a file and can directly encode the data (such as AMR and MP3 ), audiorecord is the audio stream that reads the microphone. This article uses audiorecord to read audio streams and audiotrack to play audio streams. It implements a simple hearing aid program by means of "reading and playing" and increasing the volume.

PS: because the current Android simulator does not support audiorecord, this program needs to be compiled and run on the real machine.

First paste the program running this article:

PS: The program volume adjustment only adjusts the volume inside the program. To adjust the maximum volume, you need to manually set the system volume.

To use audiorecord, you must apply for a license. In androidmanifest. XML, add the following statement:

<Uses-Permission Android: Name = "android. Permission. record_audio"> </uses-Permission>

The source code of Main. XML is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> </P> <p> <button Android: layout_height = "wrap_content" Android: Id = "@ + ID/btnrecord" <br/> Android: layout_width = "fill_parent" Android: text = "Start recording and placing"> </button> <br/> <button Android: layout_height = "wrap_content" <br/> Android: layout_width = "fill_parent" Android: text = "stop" Android: Id = "@ + ID/btnstop"> </button> <br/> <button Android: layout_height = "wrap_content" Android: Id = "@ + ID/btnexit" <br/> Android: layout_width = "fill_parent" Android: TEXT = "exit"> </button> <br/> <textview Android: Id = "@ + ID/textview01" Android: layout_height = "wrap_content" <br/> Android: TEXT = "program volume adjustment" Android: layout_width = "fill_parent"> </textview> <br/> <seekbar Android: layout_height = "wrap_content" Android: id = "@ + ID/skbvolume" <br/> Android: layout_width = "fill_parent"> </seekbar> </P> <p> </linearlayout> <br/>

The source code of testrecord. Java is as follows:

Package COM. testrecord; </P> <p> Import android. app. activity; <br/> Import android. media. audioformat; <br/> Import android. media. audiomanager; <br/> Import android. media. audiorecord; <br/> Import android. media. audiotrack; <br/> Import android. media. mediarecorder; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. widget. button; <br/> Import android. widget. seekbar; <br/> Import android. widget. toast; </P> <p> public class testrecord extends activity {<br/>/** called when the activity is first created. */<br/> button btnrecord, btnstop, btnexit; <br/> seekbar skbvolume; // adjust the volume <br/> Boolean isrecording = false; // whether the mark is recorded <br/> static final int frequency = 44100; <br/> static final int channelconfiguration = audioformat. channel_configuration_mono; <br/> static final int audioencoding = audioformat. encoding_pcm_16bit; <br/> int recbufsize, playbufsize; <br/> audiorecord; <br/> audiotrack; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> settitle ("Hearing Aid"); <br/> recbufsize = audiorecord. getminbuffersize (frequency, <br/> channelconfiguration, audioencoding); </P> <p> playbufsize = audiotrack. getminbuffersize (frequency, <br/> channelconfiguration, audioencoding); <br/> // --------------------------------------- <br/> audiorecord = new audiorecord (mediarecorder. audiosource. mic, frequency, <br/> channelconfiguration, audioencoding, recbufsize); </P> <p> audiotrack = new audiotrack (audiomanager. stream_music, frequency, <br/> channelconfiguration, audioencoding, <br/> playbufsize, audiotrack. mode_stream); <br/> // -------------------------------------------- <br/> btnrecord = (button) This. findviewbyid (R. id. btnrecord); <br/> btnrecord. setonclicklistener (New clickevent (); <br/> btnstop = (button) This. findviewbyid (R. id. btnstop); <br/> btnstop. setonclicklistener (New clickevent (); <br/> btnexit = (button) This. findviewbyid (R. id. btnexit); <br/> btnexit. setonclicklistener (New clickevent (); <br/> skbvolume = (seekbar) This. findviewbyid (R. id. skbvolume); <br/> skbvolume. setmax (100); // threshold for volume adjustment <br/> skbvolume. setprogress (70); // set the seekbar position value <br/> audiotrack. setstereovolume (0.7f, 0.7f); // set the current volume <br/> skbvolume. setonseekbarchangelistener (New seekbar. onseekbarchangelistener () {</P> <p> @ override <br/> Public void onstoptrackingtouch (seekbar) {<br/> float Vol = (float) (seekbar. getprogress ()/(float) (seekbar. getmax (); <br/> audiotrack. setstereovolume (VOL, vol); // set the volume <br/>}</P> <p> @ override <br/> Public void onstarttrackingtouch (seekbar) {<br/> // todo auto-generated method stub <br/>}</P> <p> @ override <br/> Public void onprogresschanged (seekbar, int progress, <br/> Boolean fromuser) {<br/> // todo auto-generated method stub <br/>}< br/> }); <br/>}</P> <p> @ override <br/> protected void ondestroy () {<br/> super. ondestroy (); <br/> android. OS. process. killprocess (Android. OS. process. mypid (); <br/>}</P> <p> class clickevent implements view. onclicklistener {</P> <p> @ override <br/> Public void onclick (view v) {<br/> If (V = btnrecord) {<br/> isrecording = true; <br/> New recordplaythread (). start (); // enable a thread to record and store edges <br/>} else if (V = btnstop) {<br/> isrecording = false; <br/>} else if (V = btnexit) {<br/> isrecording = false; <br/> testrecord. this. finish (); <br/>}</P> <p> class recordplaythread extends thread {<br/> Public void run () {<br/> try {<br/> byte [] buffer = new byte [recbufsize]; <br/> audiorecord. startrecording (); // start recording <br/> audiotrack. play (); // start playing </P> <p> while (isrecording) {<br/> // save data from the MIC to the buffer <br/> int bufferreadresult = audiorecord. read (buffer, 0, <br/> recbufsize); </P> <p> byte [] tmpbuf = new byte [bufferreadresult]; <br/> system. arraycopy (buffer, 0, tmpbuf, 0, bufferreadresult); <br/> // write data to play <br/> audiotrack. write (tmpbuf, 0, tmpbuf. length); <br/>}< br/> audiotrack. stop (); <br/> audiorecord. stop (); <br/>}catch (throwable t) {<br/> toast. maketext (testrecord. this, T. getmessage (), 1000); <br/>}< br/>}; <br/>}

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.