0

此代码不起作用。我添加了一些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();
    }
}
4

1 回答 1

0

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

录音在java中不起作用

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

于 2014-05-02T17:40:48.023 回答