录制语音 JAVAFX

Recording Voice JAVAFX

我再次来到这里请求您的大力帮助。

我正在尝试使用来自 Internet 的代码从麦克风录制语音。

这是对音频格式的配置:

public class microfono {

    File wavFile = new File("C:\NXB\Kamui\img\audio.wav");
    AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;
    TargetDataLine line;

    AudioFormat getAudioFormat() {
        float sampleRate = 16000;
        int sampleSizeInBits = 8;
        int channels = 2;
        boolean signed = true;
        boolean bigEndian = true;
        AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits,
                                             channels, signed, bigEndian);
        return format;
    }



public void StartRecording(ActionEvent event) throws IOException {

    try {
        AudioFormat format = getAudioFormat();
        DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);

        // checks if system supports the data line
        if (!AudioSystem.isLineSupported(info)) {
            System.out.println("Error");
            System.exit(0);
        }
        line = (TargetDataLine) AudioSystem.getLine(info);
        line.open(format);
        line.start();   // start capturing

        System.out.println("Startig...");

        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();
    }

}

此时的代码无限期地开始录音,如果我停止程序,录音将保存为路径中的 .wav 文件,但我需要在保存 wav 文件时随时停止录音的方法。

我试过这种方式但是程序停止了

   void finish() {

        line.stop();
        line.close();
        System.out.println("Finished");
    }

在此先感谢您的帮助。

我使 JavaSoundRecorder 扩展 Task。现在它应该在后台记录。

JavaSoundRecorder Class:

import java.io.*;
import javafx.concurrent.Task;
import javax.sound.sampled.*;

/**
 * A sample program is to demonstrate how to record sound in Java author:
 * www.codejava.net
 * http://www.codejava.net/coding/capture-and-record-sound-into-wav-file-with-java-sound-api
 */
public class JavaSoundRecorder extends Task<Void>
{

    // record duration, in milliseconds
    static final long RECORD_TIME = 60000;  // 1 minute

    // path of the wav file
    File wavFile = new File("RecordAudio.wav");

    // format of audio file
    AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;

    // the line from which audio data is captured
    TargetDataLine line;

    @Override
    protected Void call() throws Exception
    {
        try {
            AudioFormat format = getAudioFormat();
            DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);

            // checks if system supports the data line
            if (!AudioSystem.isLineSupported(info)) {
                System.out.println("Line not supported");
                System.exit(0);
            }
            line = (TargetDataLine) AudioSystem.getLine(info);
            line.open(format);
            line.start();   // start capturing

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

            AudioInputStream ais = new AudioInputStream(line);

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

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

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

        return null;
    }

    /**
     * Defines an audio format
     */
    AudioFormat getAudioFormat()
    {
        float sampleRate = 16000;
        int sampleSizeInBits = 8;
        int channels = 2;
        boolean signed = true;
        boolean bigEndian = true;
        AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits,
                channels, signed, bigEndian);
        return format;
    }

    /**
     * Closes the target data line to finish capturing and recording
     */
    void finish()
    {
        line.stop();
        line.close();
        System.out.println("Finished");
    }

}

主要Class:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication58 extends Application
{

 JavaSoundRecorder javaSoundRecorder;

    @Override
    public void start(Stage primaryStage)
    {       



        Button btn = new Button();
        btn.setText("Start");
        btn.setOnAction((ActionEvent event) -> {        
            if (btn.getText().equals("Start")) {  
                javaSoundRecorder  = new JavaSoundRecorder();
                Thread thread = new Thread(javaSoundRecorder);
                thread.start();

                btn.setText("Stop");
            }
            else {
                javaSoundRecorder.finish();
                javaSoundRecorder.cancel();

                btn.setText("Start");                
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}