Java录音机,不';行不通

Java录音机,不';行不通,java,eclipse,javasound,Java,Eclipse,Javasound,此代码不起作用。我添加了一些System.out.println(“开始捕获…3”)语句来理解bug所在的位置,我看到bug在行中。open(format)命令。我为什么会有虫子 import javax.sound.sampled.*; import java.io.*; public class JavaSoundRecorder { // record duration, in milliseconds static final long RECORD_TIME = 4

此代码不起作用。我添加了一些
System.out.println(“开始捕获…3”)语句来理解bug所在的位置,我看到bug在
行中。open(format)命令。我为什么会有虫子

import javax.sound.sampled.*;
import java.io.*;

public class JavaSoundRecorder {

    // record duration, in milliseconds
    static final long RECORD_TIME = 4000;  
    File wavFile = new File("C:\\Users\\kecia\\R\\RecordAudio.wav");
    AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;
    TargetDataLine line;


     AudioFormat getAudioFormat()
    {
        float sampleRate = 16000;
        //8000,11025,16000,22050,44100
        int sampleSizeInBits = 8;
        //8,16
        int channels = 2;
        //1,2
        boolean signed = true;
        //true,false
        boolean bigEndian = true;
        //true,false
        return new AudioFormat(
                sampleRate,
                sampleSizeInBits,
                channels,
                signed,
                bigEndian);
    }


    void start() {
        try {
             System.out.println("Start capturing...1");
            AudioFormat format = getAudioFormat();
            DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
            System.out.println("Start capturing...2");
            // checks if system supports the data line
            if (!AudioSystem.isLineSupported(info)) {
                System.out.println("Line not supported");
                System.exit(0);
            }
            System.out.println("Start capturing...3");
            line = (TargetDataLine) AudioSystem.getLine(info);
            System.out.println("Start capturing...4");
            ////////////////////////////////////////////////////////////////////////
            line.open(format);
            ////////////////////////////////////////////////////////////////////////
            System.out.println("Start capturing...5");
            line.start();   // start capturing


            System.out.println("Start capturing...6");

            AudioInputStream ais = new AudioInputStream(line);

            System.out.println("Start recording...");

            // start recording
            AudioSystem.write(ais, fileType, wavFile);

        } catch (LineUnavailableException ex) {
            ex.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    void finish() {
        line.stop();
        line.close();
        System.out.println("END");
    }


    public static void main(String[] args) {
        final JavaSoundRecorder recorder = new JavaSoundRecorder();

        // creates a new thread that waits for a specified
        // of time before stopping
        Thread stopper = new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(RECORD_TIME);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                recorder.finish();
            }
        });

        stopper.start();
        recorder.start();
    }
}

请参见我对类似问题的回答:

这对我来说非常合适。它捕获并保存来自麦克风的声音,并将其保存到文件中。
而且它很容易使用

请更具体地说明您的代码的作用和预期,但不会发生。您会遇到什么错误?运行这段代码对我来说很好(除了找不到文件的错误)