I'm having trouble recording audio using a thread to delay the stop
<p>I found a program on the web that records very well whit a start and stop button but i want the stop to be made using a thread to delay the procedure of stop the recording. i have try the both separate and they work fain.</p>

<p>this is the main.java</p>

<pre><code>public class main extends Activity {

    Boolean grabar = false;
    AudioRecorder recorder;
    TextView texto;
    int cont=0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        recorder = new AudioRecorder("/daniel/temp1");

        texto = (TextView) findViewById(R.id.texto);
        Button startbtn = (Button) findViewById(R.id.button);
        startbtn.setOnClickListener(new OnClickListener(){

            public void onClick(View v){


                    try {
                        recorder.start();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    background.start();




            }
           });

        Button stopbtn = (Button) findViewById(R.id.button2);
        stopbtn.setOnClickListener(new OnClickListener(){

            public void onClick(View v){

                    try {
                        recorder.stop();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }               


            }
           });


    }



    Thread background = new Thread (new Runnable() {
        public void run() {

                    cambiarHandler.sendEmptyMessage(0);
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    cambiarHandler.sendEmptyMessage(1);

            }  

     });



    Handler cambiarHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {

            switch (msg.what) {
                case 0: 
                    texto.setText("grabando");
                    break;
                case 1: 
                    texto.setText("listo");

                    try {
                        recorder.stop();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }



                    break;  
            }



        }
    };













}
</code></pre>

<p>And this is the AudioRecorder.java</p>

<pre><code>public class AudioRecorder {

  final MediaRecorder recorder = new MediaRecorder();
  final String path;

  /**
   * Creates a new audio recording at the given path (relative to root of SD card).
   */
  public AudioRecorder(String path) {
    this.path = sanitizePath(path);
  }

  private String sanitizePath(String path) {
    if (!path.startsWith("/")) {
      path = "/" + path;
    }
    if (!path.contains(".")) {
      path += ".amr";
    }
    return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
  }

  /**
   * Starts a new recording.
   */
  public void start() throws IOException {
    String state = android.os.Environment.getExternalStorageState();
    if(!state.equals(android.os.Environment.MEDIA_MOUNTED))  {
        throw new IOException("SD Card is not mounted.  It is " + state + ".");
    }

    // make sure the directory we plan to store the recording in exists
    File directory = new File(path).getParentFile();
    if (!directory.exists() &amp;&amp; !directory.mkdirs()) {
      throw new IOException("Path to file could not be created.");
    }

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(path);
    recorder.prepare();
    recorder.start();
  }

  /**
   * Stops a recording that has been previously started.
   */
  public void stop() throws IOException {
    recorder.stop();
    recorder.release();
  }

}
</code></pre>

<p>I don't know why this  doesn't work if they work separate and now i'm only making a delay.</p>

<p>thanks in advance. </p>
