Call method after 5 millisecond

nedroid picture nedroid · Mar 20, 2013 · Viewed 48.1k times · Source

How to call record method after 5 millisecond playing audio with MediaPlayer. I tried something like that but i don't know and i didn't find any good examples to end this.

while(mp.isPlaying()){
    if(record=0){
       for(int i=0; i<5millisec; i++){ //how to define 5 millisec or is any better solution
       }
    startRecord();
    record=1;
    }
}
mp.stop();
mp.release();
mp=null;   

Answer

Mr.Me picture Mr.Me · Mar 20, 2013

5 milliseconds is a very short time period and you can't limit audio output to such duration. you can use Handler to execute a delayed function but it will not ensure execution at 5 milliseconds after scheduling. a code for doing that:

Handler handler = new Handler();
handler.postDelayed(new Runnable(){
@Override
      public void run(){
        startRecord();
        mp.stop();
        mp.release();
   }
}, 5);