Android-based Recording Development and Playback

Source: Internet
Author: User
/** The application needs to have the permission to write to external storage * If the output file is written to the external storage, and also the * permission to record audio. these permissions must be set in the * application's androidmanifest. XML file, with something like: ** <uses-Permission Android: Name = "android. permission. write_external_storage "/> * <uses-Permission Android: Name =" android. Permission. record_audio "/> **/package COM. android. audiorecordtest; import android. app. activity; import android. widget. linearlayout; import android. OS. bundle; import android. OS. environment; import android. view. viewgroup; import android. widget. button; import android. view. view; import android. view. view. onclicklistener; import android. content. context; import android. util. log; import android. media. mediarecorder; Import android. media. mediaplayer; import Java. io. ioexception; public class audiorecordtest extends activity {Private Static final string log_tag = "audiorecordtest"; Private Static string mfilename = NULL; private recordbutton mrecordbutton = NULL; private mediarecorder mrecorder = NULL; private playbutton mplaybutton = NULL; private mediaplayer mplayer = NULL; private void onrecord (Boolean start) {If (start) {startrecording () ;}else {stoprecording () ;}} private void onplay (Boolean start) {If (start) {startplaying ();} else {stopplaying () ;}} private void startplaying () {mplayer = new mediaplayer (); try {mplayer. setdatasource (mfilename); mplayer. prepare (); mplayer. start ();} catch (ioexception e) {log. E (log_tag, "prepare () failed") ;}} private void stopplaying () {mplayer. relea Se (); mplayer = NULL;} private void startrecording () {mrecorder = new mediarecorder (); mrecorder. setaudiosource (mediarecorder. audiosource. MIC); mrecorder. setoutputformat (mediarecorder. outputformat. three_gpp); mrecorder. setoutputfile (mfilename); mrecorder. setaudioencoder (mediarecorder. audioencoder. amr_nb); try {mrecorder. prepare ();} catch (ioexception e) {log. E (log_tag, "prepare () failed ");} Mrecorder. start ();} private void stoprecording () {mrecorder. stop (); mrecorder. release (); mrecorder = NULL;} class recordbutton extends button {Boolean mstartrecording = true; onclicklistener clicker = new onclicklistener () {public void onclick (view v) {onrecord (mstartrecording ); if (mstartrecording) {settext ("stop recording");} else {settext ("Start recording");} mstartrecording =! Mstartrecording ;}}; public recordbutton (context CTX) {super (CTX); settext ("Start recording"); setonclicklistener (clicker );}} class playbutton extends button {Boolean mstartplaying = true; onclicklistener clicker = new onclicklistener () {public void onclick (view v) {onplay (mstartplaying); If (mstartplaying) {settext ("Stop playing");} else {settext ("start playing");} mstartplaying =! Mstartplaying ;}}; public playbutton (context CTX) {super (CTX); settext ("start playing"); setonclicklistener (clicker) ;}} public audiorecordtest () {mfilename = environment. getexternalstoragedirectory (). getabsolutepath (); mfilename + = "/audiorecordtest.3gp" ;}@ override public void oncreate (bundle icicle) {super. oncreate (icicle); linearlayout LL = new linearlayout (this); mrecordbutton = new reco Rdbutton (this); LL. addview (mrecordbutton, new linearlayout. layoutparams (viewgroup. layoutparams. wrap_content, viewgroup. layoutparams. wrap_content, 0); mplaybutton = new playbutton (this); LL. addview (mplaybutton, new linearlayout. layoutparams (viewgroup. layoutparams. wrap_content, viewgroup. layoutparams. wrap_content, 0); setcontentview (LL) ;}@ override public void onpause () {super. onpause (); If (M Recorder! = NULL) {mrecorder. Release (); mrecorder = NULL;} If (mplayer! = NULL) {mplayer. Release (); mplayer = NULL ;}}}
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.