-5

I need to record audio using AudioRecord class for a specific time. Let's say for 1 second. And then process the audio, and when the processing is done, record again for 1 second and keep repeating this until I close the program. What would be the best approach for this?

NetP
  • 9
  • 1
  • Duplicate? http://stackoverflow.com/questions/6150592/need-a-simple-example-for-audio-recording – Nate-Wilkins Aug 14 '12 at 18:23
  • That link tells you how to record, which is simple. I'm looking for a way to time the recording. How would you record sometime for 3 seconds and then do something else? – NetP Aug 14 '12 at 20:24

1 Answers1

0
public void recordandprocess()
{        

AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
            44100, AudioFormat.CHANNEL_IN_STEREO,
            AudioFormat.ENCODING_PCM_16BIT, bufferSize);

        recorder.startRecording();

        //executes run() after 1000 msec
        Handler handler = new Handler(); 
        handler.postDelayed(new Runnable() { 
         public void run() { 
                      recorder.stop();
                      recorder.release(); 
                      // do your processing here
                      recordandprocess(); //recursive call to this function to start recording after processing is done
         } 
    }, 1000); 
        .
        .
        .
      return;
}

Call this function once in onCreate() and it will continue calling itself recursively until your activity is killed by the system.

If you want to stop recursion when you press back or home button, then check a boolean inside the function before executing all the code in it. Set the boolean to false in your onPause().

Erol
  • 6,478
  • 5
  • 41
  • 55