Java录音机,不起作用

时间:2013-07-09 22:41:48

标签: java eclipse javasound

此代码不起作用。我添加了一些System.out.println("Start capturing...3");语句来了解错误的位置,我发现错误在line.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();
    }
}

1 个答案:

答案 0 :(得分:0)

请在此处查看我对类似问题的回复:

Sound recording not working in java

这对我来说非常有效。它可以捕获麦克风并将声音保存到文件中。 它很容易使用