import java.applet.*; import java.awt.*; import java.io.File; import java.applet.Applet; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import javax.sound.sampled.*; public class yoo extends Applet{ static final String Capt="capture", Stop="stop"; Font font; Button C = new Button(Capt); Button S = new Button (Stop); public yoo(){ C.setEnabled(true); S.setEnabled(false); //Register anonymous listeners C.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ C.setEnabled(false); S.setEnabled(true); //Capture input data from the // microphone until the Stop button is // clicked. captureAudio(); }//end actionPerformed }//end ActionListener );//end addActionListener() S.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent e){ C.setEnabled(true); S.setEnabled(false); //Terminate the capturing of input data // from the microphone. targetDataLine.stop(); targetDataLine.close(); }//end actionPerformed }//end ActionListener );//end addActionListener() //Put the buttons in the JFrame //Finish the GUI and make visible } int k=1; TargetDataLine targetDataLine; AudioFormat audioFormat; public void init(){ setLayout(new BorderLayout()); //создаем новый объект типа Button ( кнопка ) add("South",C); add("North",S); //задаём тип шрифта font=new Font("TimesRoman",Font.BOLD+Font.ITALIC,48); } public boolean handleEvent(Event evt){ switch(evt.id){ case Event.ACTION_EVENT : { if (( evt.arg==Capt)&&(k==2)){ k=1; new CaptureThread().start(); } else if((evt.arg==Stop)&&(k==1)){ k=2; targetDataLine.stop(); } else return false; } default: return false; } } private void captureAudio(){ try{ //Get things set up for capture audioFormat = getAudioFormat(); DataLine.Info dataLineInfo = new DataLine.Info( TargetDataLine.class, audioFormat); targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo); new CaptureThread().start(); }catch (Exception e) { e.printStackTrace(); System.exit(0); }//end catch }//end captureAudio method class CaptureThread extends Thread{ public void run(){ AudioFileFormat.Type fileType = null; File audioFile = null; //Set the file type and the file extension // based on the selected radio button. fileType = AudioFileFormat.Type.WAVE; audioFile = new File("junk.wav"); try{ targetDataLine.open(audioFormat); targetDataLine.start(); AudioSystem.write( new AudioInputStream(targetDataLine), fileType, audioFile); }catch (Exception e){ e.printStackTrace(); }//end catch }//end run }//end inner class CaptureThread private AudioFormat getAudioFormat(){ float sampleRate = 8000.0F; //8000,11025,16000,22050,44100 int sampleSizeInBits = 16; //8,16 int channels = 1; //1,2 boolean signed = true; //true,false boolean bigEndian = false; //true,false return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian); }//end getAudioFormat public static void main(String[] args) { yoo applet = new yoo (); JFrame frame = new JFrame("TestApplet"); // Для закрытия приложения: frame.getContentPane().add(applet); frame.setSize(400,450); applet.init(); applet.start(); frame.setVisible(true); } }
и html код:
<HTML> <HEAD> <TITLE>Example</TITLE> </HEAD> <BODY> <APPLET CODE='yoo.class' WIDTH=150 HEIGHT=100> </APPLET> </BODY> </HTML>
Буду рад любой помощи =)