android - AudioRecorder App crashes when I record a second time -
my app breaks down when attempt record second time when hit stop button. went through code several times. can't find problem. i'm not sure if mediarecorder or mediaplayer. works first time around. not second time. can find problem.
public class patientname extends activity implements onclicklistener { private button instructionsbtn; private imageview record; private imageview stop; private mediaplayer instructions; private mediaplayer nameplayer; private mediarecorder namerecorder; private string output_file; private button play; private button accept; private linearlayout review; private button delete; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.patient_name); output_file = environment.getexternalstoragedirectory() + "/namerecording.3gpp"; initializeviews(); initializelisteners(); } public void initializeviews() { instructionsbtn = (button)findviewbyid(r.id.patient_name_instructions); record = (imageview)findviewbyid(r.id.record_name); stop = (imageview)findviewbyid(r.id.stop_name); stop.setvisibility(view.gone); play = (button)findviewbyid(r.id.play_name); play.setvisibility(view.gone); review = (linearlayout)findviewbyid(r.id.review_name); delete = (button)findviewbyid(r.id.delete_name); accept = (button)findviewbyid(r.id.accept_name); review.setvisibility(view.gone); } public void initializelisteners() { instructionsbtn.setonclicklistener(this); record.setonclicklistener(this); stop.setonclicklistener(this); play.setonclicklistener(this); delete.setonclicklistener(this); accept.setonclicklistener(this); } @override public void onbackpressed() { } public void playinstructions() { setmaxvolume(); instructions = mediaplayer.create(patientname.this, r.raw.intro_instructions); instructions.start(); instructions.setoncompletionlistener(new oncompletionlistener() { public void oncompletion(mediaplayer play) { instructions.release(); instructionsbtn.setenabled(true); } }); } public void setmaxvolume() { audiomanager audio = (audiomanager) getsystemservice(this.audio_service); int maxvolume = audio.getstreammaxvolume(audiomanager.stream_music); audio.setstreamvolume(audiomanager.stream_music, maxvolume, 0); } public void onclick(view v) { int id = v.getid(); if (id==r.id.patient_name_instructions) { instructionsbtn.setenabled(false); playinstructions(); } if (id==r.id.record_name) { beginrecording(); } if (id==r.id.stop_name){ play.setvisibility(view.visible); namerecorder.stop(); } if (id==r.id.play_name) { playbackrecording(); } if (id==r.id.delete_name){ deleterecording(); } if (id==r.id.accept_name) { saveandcontinue(); } } private void beginrecording(){ record.setvisibility(view.gone); stop.setvisibility(view.visible); file outfile = new file(output_file); if (outfile.exists()) { outfile.delete(); }else { namerecorder = new mediarecorder(); namerecorder.setaudiosource(mediarecorder.audiosource.mic); namerecorder.setoutputformat(mediarecorder.outputformat.three_gpp); namerecorder.setaudioencoder(mediarecorder.audioencoder.amr_wb); namerecorder.setoutputfile(output_file); try { namerecorder.prepare(); } catch (illegalstateexception e) { toast toast = toast.maketext(this,"illegal state",toast.length_long); toast.show(); e.printstacktrace(); } catch (ioexception e) { toast toast = toast.maketext(this,"error recording",toast.length_long); toast.show(); e.printstacktrace(); } namerecorder.start(); } } private void playbackrecording() { play.setvisibility(view.gone); nameplayer = new mediaplayer(); try { nameplayer.setdatasource(output_file); } catch (illegalargumentexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (securityexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (illegalstateexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } try { nameplayer.prepare(); } catch (illegalstateexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } nameplayer.start(); nameplayer.setoncompletionlistener(new oncompletionlistener() { public void oncompletion(mediaplayer play) { nameplayer.release(); namerecorder.release(); review.setvisibility(view.visible); } }); } private void deleterecording() { intent = getbasecontext().getpackagemanager().getlaunchintentforpackage( getbasecontext().getpackagename() ); i.addflags(intent.flag_activity_clear_top); startactivity(i); } private void saveandcontinue() { intent intent = new intent(patientname.this, patientlocation.class); startactivity(intent); } }
here logcat:
i figured out issue. in beginrecording method, have if , else statement handles output file. unfortunately, code had in else statement wanted run no matter what.
Comments
Post a Comment