java - 录制声音JAVAFX

标签 java javafx record microphone voice

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

我正在尝试使用来自互联网的代码从麦克风录制声音。

这是音频格式的配置:

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

thisin the error when y click the stop button 预先感谢您的帮助。

最佳答案

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

JavaSoundRecorder 类:

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

}

主类:

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

}

关于java - 录制声音JAVAFX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49782559/

相关文章:

java - 如何使用 Apache POI 基于下拉选择在同一工作簿中创建多个工作表?

clojure - 记录在排序时能保证顺序吗?

visibility - 从子包中隐藏记录

java - 使用 Java 2D 创建动画效果

java - 突出显示字符串的特定实例以输出到 TextArea

java - 使用服务帐户部署多模块应用引擎项目

audio - 如何从通过远程桌面访问的 PC 的音频输入进行录制?

java - 我需要在Java中获取二维数组对象的索引

java - Eclipse 在 v. 2020-03 中生成 "source not found"

JavaFX CSS 应用于所有 ScrollPanes