规范写法
This commit is contained in:
@@ -2,128 +2,28 @@ package cn.nightsoil.liveaudio;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.application.Platform;
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.scene.media.Media;
|
||||
import javafx.scene.media.MediaPlayer;
|
||||
import javafx.stage.DirectoryChooser;
|
||||
import javafx.stage.Stage;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
@SpringBootApplication
|
||||
public class LiveAudioApplication extends Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(LiveAudioApplication.class, args);
|
||||
launch(args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) throws Exception {
|
||||
// 创建选择音频目录的标签
|
||||
Label label = new Label("音频目录");
|
||||
public void start(Stage stage) throws Exception {
|
||||
Parent root = FXMLLoader.load(getClass().getResource("/views/index.fxml"));
|
||||
|
||||
// 创建选择音频目录文本框
|
||||
TextField directoryTextField = new TextField("选择音频目录");
|
||||
directoryTextField.setEditable(false);
|
||||
|
||||
// 创建选择文件夹的按钮
|
||||
Button selectButton = new Button("选择文件夹");
|
||||
// 创建播放按钮
|
||||
Button playButton = new Button("播放");
|
||||
|
||||
// 创建文件夹选择器
|
||||
DirectoryChooser directoryChooser = new DirectoryChooser();
|
||||
// 创建MediaPlayer对象
|
||||
AtomicReference<MediaPlayer> mediaPlayer = null;
|
||||
|
||||
// 为按钮添加点击事件处理程序
|
||||
selectButton.setOnAction(event -> {
|
||||
// 显示文件夹选择器并获取用户选择的文件夹
|
||||
File selectedDirectory = directoryChooser.showDialog(primaryStage);
|
||||
if (selectedDirectory != null) {
|
||||
String folderPath = selectedDirectory.getAbsolutePath();
|
||||
// 将选择的文件夹路径放入文本框中
|
||||
directoryTextField.setText(folderPath);
|
||||
List<String> audioFiles = getAudioFiles(folderPath);
|
||||
if (!audioFiles.isEmpty()) {
|
||||
// 设置随机播放的音频文件
|
||||
String randomAudioFile = getRandomAudioFile(audioFiles);
|
||||
Media media = new Media(new File(randomAudioFile).toURI().toString());
|
||||
mediaPlayer.get().stop();
|
||||
mediaPlayer.set(new MediaPlayer(media));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 为播放按钮添加点击事件处理程序
|
||||
playButton.setOnAction(event -> {
|
||||
if (mediaPlayer.get().getStatus() == MediaPlayer.Status.PLAYING) {
|
||||
mediaPlayer.get().stop();
|
||||
}
|
||||
mediaPlayer.get().play();
|
||||
});
|
||||
|
||||
// 创建网格布局
|
||||
GridPane gridPane = new GridPane();
|
||||
gridPane.setPadding(new Insets(10));
|
||||
gridPane.setVgap(10);
|
||||
gridPane.setHgap(10);
|
||||
|
||||
// 添加控件到网格布局中
|
||||
// 第一行 音频
|
||||
gridPane.add(label, 0,0,1,1);
|
||||
gridPane.add(directoryTextField, 1, 0, 2, 1);
|
||||
gridPane.add(selectButton, 3, 0,1,1);
|
||||
|
||||
// 第二行 播放
|
||||
gridPane.add(playButton,1,1,1,1);
|
||||
|
||||
// 创建场景并设置网格布局
|
||||
Scene scene = new Scene(gridPane, 400, 100);
|
||||
|
||||
// 设置舞台标题和场景,并显示舞台
|
||||
primaryStage.setTitle("直播音频播放平台");
|
||||
primaryStage.setScene(scene);
|
||||
primaryStage.show();
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机选取一个音频文件
|
||||
* @param audioFiles
|
||||
*/
|
||||
private String getRandomAudioFile(List<String> audioFiles) {
|
||||
Random random = new Random();
|
||||
int index = random.nextInt(audioFiles.size());
|
||||
return audioFiles.get(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取目录下的音频文件列表
|
||||
* @param folderPath
|
||||
* @return
|
||||
*/
|
||||
private List<String> getAudioFiles(String folderPath) {
|
||||
List<String> audioFiles = new ArrayList<>();
|
||||
File folder = new File(folderPath);
|
||||
File[] files = folder.listFiles();
|
||||
if (files != null) {
|
||||
for (File file : files) {
|
||||
if (file.isFile()) {
|
||||
String fileName = file.getName().toLowerCase();
|
||||
if (fileName.endsWith(".mp3") || fileName.endsWith(".wav")) {
|
||||
audioFiles.add(file.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return audioFiles;
|
||||
Scene scene = new Scene(root);
|
||||
stage.setScene(scene);
|
||||
stage.show();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -131,10 +31,4 @@ public class LiveAudioApplication extends Application {
|
||||
Platform.exit();
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(LiveAudioApplication.class, args);
|
||||
launch(args);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package cn.nightsoil.liveaudio.controller;
|
||||
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.text.Text;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
/**
|
||||
* @author zhujh
|
||||
* @version 1.0
|
||||
* @className IndexController
|
||||
* @description 首页控件
|
||||
* @date 2023/7/21 23:59
|
||||
*/
|
||||
@Controller
|
||||
public class IndexController {
|
||||
|
||||
@FXML
|
||||
private Button helloButton;
|
||||
|
||||
@FXML
|
||||
private Text messageText;
|
||||
|
||||
@FXML
|
||||
private void sayHello() {
|
||||
messageText.setText("Hello!");
|
||||
}
|
||||
|
||||
}
|
||||
14
src/main/resources/views/index.fxml
Normal file
14
src/main/resources/views/index.fxml
Normal file
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import javafx.scene.control.*?>
|
||||
<?import javafx.scene.layout.*?>
|
||||
|
||||
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.14-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cn.nightsoil.liveaudio.controller.IndexController">
|
||||
<children>
|
||||
<Accordion layoutX="126.0" layoutY="75.0" />
|
||||
<Label layoutX="155.0" layoutY="96.0" prefHeight="34.0" prefWidth="64.0" text="音频目录:" />
|
||||
<TextField layoutX="219.0" layoutY="102.0" />
|
||||
<Button layoutX="393.0" layoutY="102.0" mnemonicParsing="false" text="选择文件夹" />
|
||||
<Button layoutX="155.0" layoutY="260.0" mnemonicParsing="false" text="播放" />
|
||||
</children>
|
||||
</AnchorPane>
|
||||
Reference in New Issue
Block a user