audio recording - Unable to record using AudioRecord in Android -


i developing android app. in app need record audio using audiorecord class. creating simple app test audiorecord class cause have never used before. simple app when user click "record" button start keep recording. when click "stop", stop recording. "play" button play recorded audio. app giving me error when click "record" button.

this activity

public class mainactivity extends appcompatactivity {      boolean recording;       private button btnplay,btnstop,btnrecord;     private string outputfile = null;     //outputfile = environment.getexternalstoragedirectory().getabsolutepath()+"/recording.3gp";     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         initialize();         initviews();         setupviews();     }      private void initialize()     {      }      private void initviews()     {         btnplay = (button)findviewbyid(r.id.btn_play);         btnrecord = (button)findviewbyid(r.id.btn_record);         btnstop = (button)findviewbyid(r.id.btn_stop);     }      private void setupviews()     {         btnrecord.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 startrecord();             }         });          btnstop.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 recording = false;                 toast.maketext(getbasecontext(),"stopped",toast.length_short).show();             }         });          btnplay.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 playrecord();             }         });     }      private void startrecord(){          file file = new file(environment.getexternalstoragedirectory(), "test.pcm");          try {             file.createnewfile();              outputstream outputstream = new fileoutputstream(file);             bufferedoutputstream bufferedoutputstream = new bufferedoutputstream(outputstream);             dataoutputstream dataoutputstream = new dataoutputstream(bufferedoutputstream);              int minbuffersize = audiorecord.getminbuffersize(11025,                     audioformat.channel_configuration_mono,                     audioformat.encoding_pcm_16bit);              short[] audiodata = new short[minbuffersize];              audiorecord audiorecord = new audiorecord(mediarecorder.audiosource.mic,                     11025,                     audioformat.channel_configuration_mono,                     audioformat.encoding_pcm_16bit,                     minbuffersize);              audiorecord.startrecording();              toast.maketext(getbasecontext(),"recording",toast.length_short).show();              while(recording){                 int numberofshort = audiorecord.read(audiodata, 0, minbuffersize);                 for(int = 0; < numberofshort; i++){                     dataoutputstream.writeshort(audiodata[i]);                 }             }              audiorecord.stop();             dataoutputstream.close();          } catch (ioexception e) {             e.printstacktrace();         }      }      private void playrecord(){          file file = new file(environment.getexternalstoragedirectory(), "test.pcm");          int shortsizeinbytes = short.size/byte.size;          int buffersizeinbytes = (int)(file.length()/shortsizeinbytes);         short[] audiodata = new short[buffersizeinbytes];          try {             inputstream inputstream = new fileinputstream(file);             bufferedinputstream bufferedinputstream = new bufferedinputstream(inputstream);             datainputstream datainputstream = new datainputstream(bufferedinputstream);              int = 0;             while(datainputstream.available() > 0){                 audiodata[i] = datainputstream.readshort();                 i++;             }              datainputstream.close();              audiotrack audiotrack = new audiotrack(                     audiomanager.stream_music,                     11025,                     audioformat.channel_configuration_mono,                     audioformat.encoding_pcm_16bit,                     buffersizeinbytes,                     audiotrack.mode_stream);              audiotrack.play();             audiotrack.write(audiodata, 0, buffersizeinbytes);           } catch (filenotfoundexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         }     }  } 

error throws when click record button.

this error in logcat:

enter image description here

what error? how can use audiorecord correctly?

finally solved problem. needed run new thread recording.

 private void startrecord(){          file file = new file(environment.getexternalstoragedirectory(), "test.pcm");          try {             file.createnewfile();              outputstream outputstream = new fileoutputstream(file);             bufferedoutputstream bufferedoutputstream = new bufferedoutputstream(outputstream);             dataoutputstream = new dataoutputstream(bufferedoutputstream);              final int minbuffersize = audiorecord.getminbuffersize(11025,                     audioformat.channel_configuration_mono,                     audioformat.encoding_pcm_16bit);              audiodata = new short[minbuffersize];              final audiorecord audiorecord = new audiorecord(mediarecorder.audiosource.mic,                     11025,                     audioformat.channel_configuration_mono,                     audioformat.encoding_pcm_16bit,                     minbuffersize);              audiorecord.startrecording();              toast.maketext(getbasecontext(),"recording",toast.length_short).show();               recordingthread = new thread(new runnable() {                 public void run() {                     while(recording){                         int numberofshort = audiorecord.read(audiodata, 0, minbuffersize);                         for(int = 0; < numberofshort; i++){                             try{                                 dataoutputstream.writeshort(audiodata[i]);                             }                             catch (ioexception e)                             {                                 toast.maketext(getbasecontext(),e.getmessage(),toast.length_short).show();                             }                         }                     }                 }             }, "audiorecorder thread");              audiorecord.stop();             dataoutputstream.close();          } catch (ioexception e) {             e.printstacktrace();         }      } 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -