Applet de captura de mic

0 respostas
F

OI gente, alguem poderia me ajudar com o seguinte programa:

/*File AudioRecorder02.java
Copyright 2003, Richard G. Baldwin

This program demonstrates the capture of audio
data from a microphone into an audio file.

A GUI appears on the screen containing the
following buttons:
  Capture
  Stop

In addition, five radio buttons appear on the
screen allowing the user to select one of the
following five audio output file formats:

  AIFC
  AIFF
  AU
  SND
  WAVE

When the user clicks the Capture button, input
data from a microphone is captured and saved in
an audio file named junk.xx having the specified
file format.  (xx is the file extension for the
specified file format.  You can easily change the
file name to something other than junk if you
choose to do so.)

Data capture stops and the output file is closed
when the user clicks the Stop button.

It should be possible to play the audio file
using any of a variety of readily available
media players, such as the Windows Media Player.

Not all file types can be created on all systems.
For example, types AIFC and SND produce a "type
not supported" error on my system.

Be sure to release the old file from the media
player before attempting to create a new file
with the same extension.

Tested using SDK 1.4.1 under Win2000
************************************************/

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.TimerTask;
import java.util.Timer;

import javax.sound.sampled.*;

public class AudioRecorder extends JApplet {//Inicia a aplet
		
		private static boolean stop;
		AudioFormat audioFormat;
		TargetDataLine targetDataLine;
		
		Timer timer=new Timer();
		Toolkit toolkit;	    
		public static int i;
	public void init (){
		stop=false;
		
	}
	

	public void start(){//Start a applet
		new AudioRecorder();//chama o audio recorder method
		 System.out.println("applet iniciada");
  }//ending start
  


		
	

	public AudioRecorder(){//constructor do audio recorder


    while(true){//loop infinito pra capturar o audio
          captureAudio();
          stop=temp(2);//chama o timer
          if(stop=true){;//se o stop retornado pelo timer for verdadeira realiza o salvamento do audio
          
          targetDataLine.stop();
           targetDataLine.close();
           i++;//Incremento do I
           if (i<10){
         	  i=0;
           } 
           stop=false; 
          }
         }

 
  }//end constructor

  //This method captures audio input from a
  // microphone and saves it in an audio file.
  private void captureAudio(){
    try{
      //Get things set up for capture
      audioFormat=new AudioFormat(8000.0F,// sampleRate 8000,11025,16000,22050,44100
    		  		16, //sampleSizeInBits 8,16
    		  		2,//channels,1,2
    		  		true,// signed, true,false
    		  		false);//bigEndian true,false
      
      final DataLine.Info dataLineInfo =
                          new DataLine.Info(
                            TargetDataLine.class,
                            audioFormat);
      targetDataLine = (TargetDataLine)
               AudioSystem.getLine(dataLineInfo);

      //Create a thread to capture the microphone
      // data into an audio file and start the
      // thread running.  It will run until the
      // Stop button is clicked.  This method
      // will return after starting the thread.
      new CaptureThread().start();
    }catch (final Exception e) {
      e.printStackTrace();
      System.exit(0);
    }//end catch
  }//end captureAudio method

  

//Inner class to capture data from microphone
// and write it to an output audio file.
class CaptureThread extends Thread{
  public void run(){
    final AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;//COLOCAR AKI O TIPO
    final File audioFile = new File("c:/audio"+i+"wav");	//COLOCAR AKI OS NOMES
 
    try{
      targetDataLine.open(audioFormat);
      targetDataLine.start();
      AudioSystem.write(//AKI EH OND SALVA
            new AudioInputStream(targetDataLine),
            fileType,
            audioFile);
    }catch (final Exception e){
      e.printStackTrace();
    }//end catch

  }//end run
} //end inner class CaptureThread
//=============================================//

public boolean temp (final int seconds) {//função temporizadora esecuta a task dpois d seconds e continua de 2 em 2 segundos
	try{
	timer.schedule(new Task(), seconds*1000l,2000l);
    return stop;

}
catch (final Exception e){
	e.printStackTrace();
}
	return rootPaneCheckingEnabled;
}
class Task extends TimerTask {
    public void run() {
    	stop=true;

    }
}
}//end outer class AudioRecorder.java

ele foi adaptado deste programa:http://www.developer.com/java/other/article.php/2105421

Criado 14 de novembro de 2006
Respostas 0
Participantes 1