From 3699bbd2f4de016fee073f691912d35f7b69aeac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cnight=E2=80=9D?= Date: Sun, 23 Jul 2023 02:03:03 +0800 Subject: [PATCH] =?UTF-8?q?=EF=BC=88=E6=9C=AA=E9=85=8D=E7=BD=AE=E6=88=90?= =?UTF-8?q?=E5=8A=9F=EF=BC=89=E9=85=8D=E7=BD=AE=E6=9C=AC=E5=9C=B0=E5=BA=93?= =?UTF-8?q?=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 36 ++++-- .../config/LibraryPathConfigurer.java | 27 ++++ .../liveaudio/controller/IndexController.java | 121 +++++++++++++++++- src/main/resources/application.properties | 17 +++ src/main/resources/views/index.fxml | 4 +- 5 files changed, 189 insertions(+), 16 deletions(-) create mode 100644 src/main/java/cn/nightsoil/liveaudio/config/LibraryPathConfigurer.java diff --git a/pom.xml b/pom.xml index 691a800..2c11281 100644 --- a/pom.xml +++ b/pom.xml @@ -2,23 +2,28 @@ 4.0.0 + org.springframework.boot spring-boot-starter-parent 2.5.3 + cn.nightsoil live-audio - 0.0.1-SNAPSHOT + 0.0.1 live-audio live-audio + 11 - 2.5.3 - 11 - 11 + ${java.version} + ${java.version} + 11 + cn.nightsoil.liveaudio.LiveAudioApplication + @@ -26,7 +31,7 @@ spring-boot-starter - + org.springframework.boot spring-boot-starter-web @@ -36,29 +41,42 @@ org.openjfx javafx-controls - 11 + ${javafx.version} org.openjfx javafx-fxml - 11 + ${javafx.version} - + + org.openjfx javafx-maven-plugin 0.0.7 - cn.nightsoil.liveaudio.LiveAudioApplication + ${exec.mainClass} + + org.springframework.boot spring-boot-maven-plugin + + + + org.apache.maven.plugins + maven-compiler-plugin + + ${java.version} + ${java.version} + + diff --git a/src/main/java/cn/nightsoil/liveaudio/config/LibraryPathConfigurer.java b/src/main/java/cn/nightsoil/liveaudio/config/LibraryPathConfigurer.java new file mode 100644 index 0000000..cce00c8 --- /dev/null +++ b/src/main/java/cn/nightsoil/liveaudio/config/LibraryPathConfigurer.java @@ -0,0 +1,27 @@ +package cn.nightsoil.liveaudio.config; + +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.core.env.Environment; + +/** + * @author zhujh + * @version 1.0 + * @className LibraryPathConfigurer + * @description 程序启动时,加载系统属性 + * @date 2023/7/23 0:34 + */ +public class LibraryPathConfigurer implements ApplicationRunner { + + private final Environment environment; + + public LibraryPathConfigurer(Environment environment) { + this.environment = environment; + } + + @Override + public void run(ApplicationArguments args) throws Exception { + String libraryPath = environment.getProperty("app.library.path"); + System.setProperty("java.library.path", libraryPath); + } +} diff --git a/src/main/java/cn/nightsoil/liveaudio/controller/IndexController.java b/src/main/java/cn/nightsoil/liveaudio/controller/IndexController.java index 05b09dc..1c766c5 100644 --- a/src/main/java/cn/nightsoil/liveaudio/controller/IndexController.java +++ b/src/main/java/cn/nightsoil/liveaudio/controller/IndexController.java @@ -3,10 +3,15 @@ package cn.nightsoil.liveaudio.controller; import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.TextField; +import javafx.scene.media.Media; +import javafx.scene.media.MediaPlayer; import javafx.stage.DirectoryChooser; import org.springframework.stereotype.Controller; import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; /** * @author zhujh @@ -17,23 +22,129 @@ import java.io.File; */ @Controller public class IndexController { - + + /** + * 目录选择器按钮 + */ @FXML private Button selectAudioFolder; - + + /** + * 获取音频目录地址 + */ @FXML private TextField audioPathText; - + + /** + * 音频播放按钮 + */ + @FXML + private Button playButton; + + /** + * 存储音频文件路径的列表 + */ + private List audioFileList; + + /** + * 媒体播放器对象 + */ + private MediaPlayer mediaPlayer; + + /** + * 选择音频目录 + */ @FXML private void getAudioFolder() { // 创建文件夹选择器 DirectoryChooser directoryChooser = new DirectoryChooser(); directoryChooser.setTitle("选择音频目录"); + // 显示文件夹选择器并获取用户选择的文件夹 File selectAudioDir = directoryChooser.showDialog(audioPathText.getScene().getWindow()); if (selectAudioDir != null) { - + // 获取用户选择的文件夹路径 + String absolutePath = selectAudioDir.getAbsolutePath(); + // 将选择的文件夹路径放到文本框中 + audioPathText.setText(absolutePath); + // 获取所选目录下的音频文件 + retrieveAudioFiles(absolutePath); } } - + + /** + * 添加音频文件(检索) + * @param absolutePath + */ + private void retrieveAudioFiles(String absolutePath) { + File directory = new File(absolutePath); + + // 确保选择的是目录 + if (directory.isDirectory()) { + // 初始化音频文件路径列表 + audioFileList = new ArrayList<>(); + + File[] files = directory.listFiles((dir, name) -> { + String lowerCaseName = name.toLowerCase(); + // 过滤出以.mp3或.wav结尾的文件 + return lowerCaseName.endsWith(".mp3") || lowerCaseName.endsWith(".wav"); + }); + + if (files != null && files.length > 0) { + for (File file : files) { + // 将音频文件的绝对路径添加到列表中 + audioFileList.add(file.getAbsolutePath()); + } + } + } + } + + /** + * 切换播放 + */ + public void togglePlayback() { + if (mediaPlayer != null && mediaPlayer.getStatus() == MediaPlayer.Status.PLAYING) { + // 如果正在播放,则暂停 + mediaPlayer.pause(); + // 修改按钮文字为 "播放" + playButton.setText("播放"); + } else { + // 开始播放音频 + startPlayback(); + // 修改按钮文字为 "暂停" + playButton.setText("暂停"); + } + } + + private void startPlayback() { + if (audioFileList != null && !audioFileList.isEmpty()) { + Random random = new Random(); + int randomIndex = random.nextInt(audioFileList.size()); + String randomAudioPath = audioFileList.get(randomIndex); + + // 创建Media对象,传入随机选择的音频文件路径 + Media media = new Media(new File(randomAudioPath).toURI().toString()); + + if (mediaPlayer != null) { + // 如果有正在播放的音频,先停止播放 + mediaPlayer.stop(); + } + + // 创建MediaPlayer对象并传入Media对象 + mediaPlayer = new MediaPlayer(media); + // 音频播放完成时的监听器 + mediaPlayer.setOnEndOfMedia(() -> { + // 修改按钮文字为 "播放" + playButton.setText("播放"); + // 停止音频播放 + mediaPlayer.stop(); + // 释放MediaPlayer资源 + mediaPlayer.dispose(); + }); + + // 播放音频 + mediaPlayer.play(); + } + } + } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b13789..c9684e9 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,18 @@ +# 配置JavaFX运行环境 +spring.main.web-application-type=none +spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration +# 配置JavaFX的启动类 +spring.application.ui=cn.nightsoil.liveaudio.LiveAudioApplication + +# 配置JavaFX视图文件的路径 +spring.fxml.view-location=classpath:/views/ + +# 配置JavaFX控制器的包名 +spring.fxml.controller-package=cn.nightsoil.liveaudio.controller + +# 配置本地库路径 +app.library.path=src/main/lib/javafx-sdk-11.0.2/lib + +# 配置JavaFX资源文件的路径 +# spring.resources.static-locations=classpath:/static/ diff --git a/src/main/resources/views/index.fxml b/src/main/resources/views/index.fxml index 3851d91..33e7552 100644 --- a/src/main/resources/views/index.fxml +++ b/src/main/resources/views/index.fxml @@ -8,7 +8,7 @@