Android Recording related

Source: Internet
Author: User

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.

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.