Android simple Recorder

Source: Internet
Author: User

Today, when I was working on a simple recorder, the recorder was always unable to be instantiated and used normally. Later I searched for problems, tried them one by one, and finally confirmed that the two settings had been reversed, however, it is depressing that we cannot work normally. I found a tutorial and did not mention it in the tutorial. The two cannot be reversed. Just give yourself a warning and hope to see it, people who do not know this problem can remember. NextCodePut it below:

 

Package COM. recordtest; import Java. io. file; import Java. io. filenamefilter; import Java. io. ioexception; import Java. util. arraylist; import Java. util. list; import android. app. listactivity; import android. media. mediarecorder; import android. OS. bundle; import android. OS. environment; import android. util. log; import android. view. view; import android. widget. arrayadapter; import android. widget. button; import android. wi DGET. toast; public class recordtest extends listactivity {button btn1, btn2; int currertindex = 0; private mediarecorder; // create a recorder file recordfile = NULL; file sdcardpath = NULL; // recording file directory string tempfile = "record_temp _"; List <string> filelist = new arraylist <string> ();/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (S Avedinstancestate); setcontentview (R. layout. main); If (environment. getexternalstoragestate (). equals (Android. OS. environment. media_mounted) {sdcardpath = environment. getexternalstoragedirectory (); settitle (sdcardpath. getname ();} else {toast. maketext (recordtest. this, "No SD card", 1000 ). show () ;}getrecordlist (); btn1 = (button) findviewbyid (R. id. btn1); btn2 = (button) findviewbyid (R. id. btn2); btn1.setonclickli Stener (new view. onclicklistener () {public void onclick (view v) {// todo auto-generated method stubtry {recordfile = file. createtempfile (tempfile ,". amr ", sdcardpath); log. I ("AAAAA", "1"); mediarecorder = new mediarecorder (); log. I ("AAAAA", "2"); mediarecorder. setaudiosource (mediarecorder. audiosource. MIC); // set the microphone log. I ("AAAAA", "3"); mediarecorder. setoutputformat (mediarecorder. outputformat. default); // set Output file format log. I ("AAAAA", "4"); mediarecorder. setaudioencoder (mediarecorder. audioencoder. default); // set the encoding format log. I ("AAAAA", "5"); mediarecorder. setoutputfile (recordfile. getabsolutepath (); // use the absolute path to save the file log. I ("AAAAA", "6"); mediarecorder. prepare (); mediarecorder. start (); toast. maketext (recordtest. this, "Start", toast. length_long ). show ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktr ACE () ;}}); btn2.setonclicklistener (New View. onclicklistener () {public void onclick (view v) {// todo auto-generated method stubtry {If (recordfile! = NULL) {mediarecorder. stop (); filelist. add (recordfile. getname (); arrayadapter <string> List = new arrayadapter <string> (recordtest. this, R. layout. itemlist, R. id. listitem, filelist); setlistadapter (list); mediarecorder = NULL; toast. maketext (recordtest. this, "stop", toast. length_long ). show () ;}} catch (exception e) {log. I ("eeeee", E. getmessage () ;}}) ;}void getrecordlist () {// first check whether sdcardtry {file home = sdcardpath; filelist. clear (); If (home. list (New fileter ()). length> 0) {for (File file: Home. listfiles (New fileter () {filelist. add (file. getname ();} arrayadapter <string> List = new arrayadapter <string> (this, R. layout. itemlist, R. id. listitem, filelist); setlistadapter (list) ;}} catch (exception e) {toast. maketext (recordtest. this, E. getmessage (), 1000 ). show () ;}} class fileter implements filenamefilter {public Boolean accept (File Dir, string filename) {// todo auto-generated method stubreturn filename. equals (". amr ");}}}

during usage, you must add the permission to operate the SD card and use the recording device, that is, in androidmanifest. add in XML:


then you can use it. If you have any questions, please let us know!

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.