Android microphones play a big role in today's life, calling, video chatting, voice recognition and more.
The Android SDK API provides a convenient way to call, write a small demo below.
Five buttons: Start recording, stop, play, delete recording, save (make Save As)
Recordbtn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Try{_recordaudiofile=file.createtempfile ("Record", ". Amr"); //will be created with a record beginning, followed by a string of numbers. Amr_mediarecorder=NewMediarecorder (); _mediarecorder.setaudiosource (MediaRecorder.AudioSource.MIC);//Microphone _mediarecorder.setoutputformat (Media Recorder.OutputFormat.DEFAULT); _mediarecorder.setaudioencoder (MediaRecorder.AudioEncoder.DEFAULT); _mediarecorder.setoutputfile (_recordaudiofile.getabsolutepath ()); _mediarecorder.prepare (); _mediarecorder.start (); }Catch(IOException e) {e.printstacktrace (); } } }); Stopbtn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {if(_mediarecorder!=NULL) {_mediarecorder.stop (); _mediarecorder.release (); _mediarecorder=NULL; } } }); Playbtn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {if(_recordaudiofile==NULL||!_recordaudiofile.exists ()) {Toast.maketext (Getapplicationcontext (),"Not recorded", 0). Show (); return; } _mediaplayer=NewMediaPlayer (); Try{_mediaplayer.setdatasource (_recordaudiofile.getabsolutepath ()); _mediaplayer.prepare (); _mediaplayer.start (); }Catch(IOException e) {e.printstacktrace (); } } }); Deletebtn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {_recordaudiofile.delete (); } }); Savebtn.setonclicklistener (NewOnclicklistener () {//caveats: GetAbsolutePath () Return string does not contain "/" at the end@Override Public voidOnClick (View v) {String sdcard=environment.getexternalstoragedirectory (). toString (); File dest=NewFile (sdcard+ "/myaudiorecord/"); if(!dest.exists ()) Dest.mkdir (); ShortRtncode=MoveFile (_recordaudiofile.getabsolutepath (), Dest.getabsolutepath ()+"/"+_recordaudiofile.getname ()); if(rtncode==0) Toast.maketext (Getapplicationcontext (),"Successfully saved to/sdcard/myaudiorecord/", 1). Show (); Else if(rtncode==1) Toast.maketext (Getapplicationcontext (),"Not recorded, save failed", 0). Show (); Else if(rtncode==2) Toast.maketext (Getapplicationcontext (),"File with the same name already exists, save failed", 1). Show (); } }); } Private ShortmoveFile (String oldpath,string newpath) {File oldFile=NewFile (OldPath); if(!oldfile.exists ())return1; File NewFile=NewFile (NewPath); if(Newfile.exists ()) {return2;} Else Try{newfile.createnewfile (); FileInputStream FIS=NewFileInputStream (OldFile); FileOutputStream Fos=NewFileOutputStream (NewFile); byte[]buf=New byte[1024]; intbytes; while((Bytes=fis.read (BUF))!=-1) {fos.write (buf,0, bytes); } fis.close (); Fos.close (); //oldfile.delete ();//delete old files return0; } Catch(Exception e) {e.printstacktrace (); return5; }
Don't forget the permissions you have under:
<uses-permission android:name= "Android.permission.RECORD_AUDIO"/>
<!--permission to write data to SDcard
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
<!--permissions to create/delete files in SDcard-
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
The above code simply saves the recorded audio to a specific location, and does not make a detailed error check, such as judging the status of the SD card.
Thus can do some simple application, such as for people to amuse themselves to record their songs, by compressing or transcoding, and then uploaded to the server, to others to share ...
But when it comes to dealing with sound, bloggers are really stretched out, the first few daily lame encoder converts the recorded WAV format to small size and loss of sound quality MP3 when the parameters are ignorant.
If you are interested to try, Lame encoder is open source C language code, can be ported to the phone.