如何使用fxml javafx运行recorder.java



我想使用UI FXML记录语音。但是我不知道如何在我创建的UI FXML上运行此Java文件。

javarecorder.java

package application;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;
public class JavaRecorder {
     static final long RECORD_TIME = 30000;  // 0.5 minute
        // path of the wav file
        File wavFile = new File("C:/Users/Asus/Music/RecordAudio.wav");
        // format of audio file
        AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;
        // the line from which audio data is captured
        TargetDataLine line;
        /**
         * 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;
        }
        /**
         * Captures the sound and record into a WAV file
         */
        void start() {
            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 ex) {
                ex.printStackTrace();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
        /**
         * Closes the target data line to finish capturing and recording
         */
        void finish() {
            line.stop();
            line.close();
            System.out.println("Finished");
        }
        /**
         * Entry to run the program
         */
        public static void main(String[] args) {
            final JavaRecorder recorder = new JavaRecorder();
            // 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();
            // start recording
            recorder.start();
        }
    }

下面是为UI FXML编码。

main.java

package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.fxml.FXMLLoader;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource("First.fxml"));
            Scene scene = new Scene(root,717,700);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        launch(args);
    }
}

我想通过单击我在ayatp1.fxml中创建的"记录"按钮来录制。

AYATP1CONTROLLER.JAVA

package application;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.media.MediaPlayer;
import javafx.util.Duration;
public class AyatP1Controller implements Initializable {
    @FXML
    private AnchorPane rootPane;
    @Override
    public void initialize(URL url, ResourceBundle rb){
         mediaPlayer.setOnEndOfMedia(()->button.setVisible(true));
    }
    @FXML
    private void loadPrev(ActionEvent event)throws IOException {
        AnchorPane pane = (AnchorPane)FXMLLoader.load(getClass().getResource("MenuAyat.fxml"));
        rootPane.getChildren().setAll(pane);
    }
    @FXML
    private void loadNext(ActionEvent event)throws IOException {
        AnchorPane pane = (AnchorPane)FXMLLoader.load(getClass().getResource("AyatP2.fxml"));
        rootPane.getChildren().setAll(pane);
    }
    @FXML private Button button;
    @FXML private Button rekod;
    @FXML private MediaPlayer mediaPlayer;

    @FXML
    public void Playbtn(ActionEvent event){
        button.setVisible(false);
        mediaPlayer.seek(Duration.ZERO);
        mediaPlayer.play();
    }
    @FXML
    public void Playrecord(ActionEvent event){
        rekod.setVisible(false);
    }
}

ayatp1.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.media.Media?>
<?import javafx.scene.media.MediaPlayer?>
<?import javafx.scene.media.MediaView?>
<AnchorPane fx:id="rootPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="598.0" prefWidth="717.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.AyatP1Controller">
   <children>
      <ImageView fitHeight="598.0" fitWidth="761.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../../../../../Pictures/gambar/classroom.jpg" />
         </image>
      </ImageView>
      <ImageView fitHeight="260.0" fitWidth="385.0" layoutX="55.0" layoutY="94.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../../../../../Pictures/gambar/Slide1.jpg" />
         </image>
      </ImageView>
      <Button layoutX="411.0" layoutY="525.0" mnemonicParsing="false" onAction="#loadPrev">
         <graphic>
            <ImageView fitHeight="48.0" fitWidth="48.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@../../../../../Pictures/gambar/left_circle.png" />
               </image>
            </ImageView>
         </graphic>
      </Button>
      <Button layoutX="482.0" layoutY="525.0" mnemonicParsing="false" onAction="#loadNext">
         <graphic>
            <ImageView fitHeight="48.0" fitWidth="48.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@../../../../../Pictures/gambar/right_circle.png" />
               </image>
            </ImageView>
         </graphic>
      </Button>
      <Button fx:id="button" layoutX="125.0" layoutY="439.0" mnemonicParsing="false" onAction="#Playbtn" text="Start" />
      <Button fx:id="rekod" layoutX="222.0" layoutY="439.0" mnemonicParsing="false" onAction="#Playrecord" text="Record" />
      <Button layoutX="324.0" layoutY="439.0" mnemonicParsing="false" text="Stop" />
       <MediaView>
                  <mediaPlayer>
                      <MediaPlayer fx:id="mediaPlayer" autoPlay="false">
                          <media>
                              <Media source="file:///C:/Users/Asus/Music/gitarresms.mp3" />
                          </media>
                      </MediaPlayer>
                  </mediaPlayer>    
      </MediaView>
   </children>
</AnchorPane>

您可能想尝试

AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource("/First.fxml"));

"First.fxml"更改为"/First.fxml",然后从Main类运行main(String[] args)方法。

最新更新