audio recording - Record MIC sound into byte array in android -


i'm trying record mic direcly short array.

the goal not write file audio track, save within short array.

if've tried several methods , best i've found recording audiorecord , play audiotrack. i've found class here:

android: need record mic input

this class makes need, have modify achieve desired result, but...i don't well, i'm missing something...

here's modification (not working @ all):

private class audio extends thread {         private boolean stopped = false;         /**          * give thread high priority it's not canceled unexpectedly, , start          */         private audio()         {              android.os.process.setthreadpriority(android.os.process.thread_priority_urgent_audio);             start();         }          @override         public void run()         {              log.i("audio", "running audio thread");             audiorecord recorder = null;             audiotrack track = null;             //short[][]   buffers  = new short[256][160];             int ix = 0;              /*              * initialize buffer hold continuously recorded audio data, start recording, , start              * playback.              */             try             {                 int n = audiorecord.getminbuffersize(8000,audioformat.channel_in_mono,audioformat.encoding_pcm_16bit);                 recorder = new audiorecord(audiosource.mic, 8000, audioformat.channel_in_mono, audioformat.encoding_pcm_16bit, n*10);                 short[] buff = new short[n];                 recorder.startrecording();                 /*                  * loops until outside of thread stops it.                  * reads data recorder , writes audio track playback.                  */                 while(!stopped) {                       //log.i("map", "writing new data buffer");                     //short[] buffer = buffer[ix++ % buffer.length];                     n = recorder.read(buff, 0, buff.length);                 }                  recorder.stop();                 recorder.release();                  track = new audiotrack(audiomanager.stream_music, 8000,                          audioformat.channel_out_mono, audioformat.encoding_pcm_16bit, n*10, audiotrack.mode_stream);                 track.play();                  (int =0; i< buff.length;i++) {                     track.write(buff, i, buff.length);                 }               } catch(exception x)    {                  //log.e("audio", x.getmessage());                 x.printstacktrace();             } {                   track.stop();                 track.release();             }         }          /**          * called outside of thread in order stop recording/playback loop          */         private void close()         {              stopped = true;         }      } 

what need record sound in short array buffer , when user push button, play it...but right now, i'm trying record sound and, when user push button, recording stop , sound start playing...

anyone can me?

thanks.

you need restructure code want do. if understand correctly want read sound until 'stopped' set true, play data.

just understand potentially lot of buffered data depending on how long recording time is. write file or store series of buffers abstract data type.

just work create vector of short [] , allocate new short [] buffer in 'while(!stopped)' loop , stuff vector.

after while loop stops can iterate through vector , write buffers audiotrack.

as understand, blip hearing last 20ms or of audio since buffer kept last little bit.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -