首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用fxml recorder.java运行JavaFX?

如何使用fxml recorder.java运行JavaFX?
EN

Stack Overflow用户
提问于 2017-06-05 03:37:38
回答 1查看 303关注 0票数 1

我想使用UI记录声音。但是我不知道如何在我创建的UI fxml上运行这个java文件。

JavaRecorder.java

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
<?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>
EN

回答 1

Stack Overflow用户

发布于 2017-06-05 06:22:56

你也许想试试

代码语言:javascript
运行
复制
AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource("/First.fxml"));

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44361333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档