Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call method after 5 millisecond

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;   
like image 241
nedroid Avatar asked Mar 20 '13 11:03

nedroid


People also ask

How do you call a method after a delay?

This example demonstrates how do I call method after a delay in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How to call a function repeatedly after fixed time interval in Java?

The setInterval() method calls a function at specified intervals (in milliseconds). The setInterval() method continues calling the function until clearInterval() is called, or the window is closed. 1 second = 1000 milliseconds.

How do you call a function in Android?

To call a method in Java, you type the method's name, followed by brackets. This code simply prints “Hello world!” to the screen. Therefore, any time we write helloMethod(); in our code, it will show that message to the screen.


1 Answers

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);
like image 155
Mr.Me Avatar answered Sep 18 '22 19:09

Mr.Me