Android-based Recording Development and Playback
Last Update:2018-12-07
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 ;}}}