محمد
محمد

Reputation: 139

sleep between two function in android java?

I Have class to record sound in my android project . In this class i have 2 function : start() and stop() and i have a main class I want run it from other activity such : record(second) and record some time and stop aoutomaticly after those second ! Like this :

start();
//sleep(second)
stop();

How I can do this ? this is my code :

package com.example.voicerec;

import java.io.IOException;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.view.View;

public class VoiceR {

private MediaRecorder myRecorder;
private MediaPlayer   myPlayer;
private String        outputFile = null;



public void Record()
{
    outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/momeni.3gpp";
    myRecorder = new MediaRecorder();
    myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.THREE_GPP);
    myRecorder.setOutputFile(outputFile);

}



public void start(View view) {
    try {
        myRecorder.prepare();
        myRecorder.start();
    }
    catch (IllegalStateException e) {
        // start:it is called before prepare()
        // prepare: it is called after start() or before setOutputFormat() 
        e.printStackTrace();
    }
    catch (IOException e) {
        // prepare() fails
        e.printStackTrace();
    }

}



public void stop(View view) {
    try {
        myRecorder.stop();
        myRecorder.release();
        myRecorder = null;

    }
    catch (IllegalStateException e) {
        //  it is called before start()
        e.printStackTrace();
    }
    catch (RuntimeException e) {
        // no valid audio/video data has been received
        e.printStackTrace();
    }
}



public void play(View view) {
    try {
        myPlayer = new MediaPlayer();
        myPlayer.setDataSource(outputFile);
        myPlayer.prepare();
        myPlayer.start();

    }
    catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}



public void stopPlay(View view) {
    try {
        if (myPlayer != null) {
            myPlayer.stop();
            myPlayer.release();
            myPlayer = null;

        }
    }
    catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

Upvotes: 0

Views: 138

Answers (1)

Muhammed Refaat
Muhammed Refaat

Reputation: 9103

Just make a thread that runs start then sleeps for record time then runs stop :

    final int time = second * 1000;
    Thread sound = new Thread(new Runnable() {

        @Override
        public void run() {

            start(view);
            try{
                Thread.sleep(time);
            }catch(Exception e){}
            stop();
        }
    });

sound.start();

or you can use CountDownLatch :

    CountDownLatch latch = new CountDownLatch(1);
    start(view);

    Thread stopping = new Thread(new Runnable() {

        @Override
        public void run() {

            try {
                latch.await();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            stop(view);
        }
    });
    stopping.start();

and after the recording process finished:

 latch.countDown();

Upvotes: 1

Related Questions