Question

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;   
Was it helpful?

Solution

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);

OTHER TIPS

You can use the method postDelayed. In the example below I run my routine 100 millis after to call the method.

new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        barVolume.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
                    }
                }, 
                100);

try this:

//Auto Start after 2 seconds
        if(ENABLE_AUTO_START) {
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    // this code will be executed after 2 seconds
                    doThis();
                }
            }, 2000);
        }

Perhaps you want to use Thread.sleep?

Like so:

if(record == 0){
   Thread.sleep(5);
}

Notice that I used == in the if statement to check for equality, rather than assigning the value of 0 each time, I assume this is what you want.

It is worth mentioning that putting a Thread to sleep will stop it doing anything for the duration that you specify. If this is a UI Thread, then you will effectively "freeze" the UI for that duration, so make sure you are using it appropriately. Hwoever, you example for loop indicates this is exactly the kind of thing you are attempting to do.

You could try using Thread.sleep(5), or, if you don't want to use the UI thread for this, you could use a Timer, or an AsyncTask which triggers a callback after waiting 5ms in the doInBackground() method.

Here is a pretty good example for using Timer: https://stackoverflow.com/a/4598737/832008

You can also use ScheduledExecutorService

Using an ExecutorService, you can schedule commands to run after a given delay, or to execute periodically. The following example shows a class with a method that sets up a ScheduledExecutorService to beep every ten seconds for an hour:

import static java.util.concurrent.TimeUnit.*;
 class BeeperControl {
   private final ScheduledExecutorService scheduler =
     Executors.newScheduledThreadPool(1);

   public void beepForAnHour() {
     final Runnable beeper = new Runnable() {
       public void run() { System.out.println("beep"); }
     };
     final ScheduledFuture<?> beeperHandle =
       scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
     scheduler.schedule(new Runnable() {
       public void run() { beeperHandle.cancel(true); }
     }, 60 * 60, SECONDS);
   }
 }
public static MediaRecorder mRecorder = new MediaRecorder();

public void startRecording(String fileName) {
    if(mRecorder != null) {
        try {
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mRecorder.setOutputFile(fileName);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

            try {
                mRecorder.prepare();
            } catch (IOException e) {
                Log.e(StartPhoneCallService.class.getSimpleName(), "prepare() failed");
            }
            mRecorder.start();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }
}

public void stopRecording() {
    if(mRecorder != null) {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
    }
}

Now you can call the Handler to play 5 millisecond

private final int recording_time = 5;

Handler handler = new Handler();
handler.postDelayed(new Runnable(){
  @Override
  public void run() {
     startRecording("YOUR FILE NAME");
     // Stop your recording after 5 milliseconds
     stopRecording();
   }
}, recording_time );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top