(未配置成功)配置本地库路径
This commit is contained in:
36
pom.xml
36
pom.xml
@@ -2,23 +2,28 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.5.3</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<groupId>cn.nightsoil</groupId>
|
||||
<artifactId>live-audio</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<version>0.0.1</version>
|
||||
<name>live-audio</name>
|
||||
<description>live-audio</description>
|
||||
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
<spring-boot.version>2.5.3</spring-boot.version>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
<javafx.version>11</javafx.version>
|
||||
<exec.mainClass>cn.nightsoil.liveaudio.LiveAudioApplication</exec.mainClass>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- Spring Boot 核心依赖 -->
|
||||
<dependency>
|
||||
@@ -26,7 +31,7 @@
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Boot Web Starter(如果需要) -->
|
||||
<!-- Spring Boot Web Starter -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
@@ -36,29 +41,42 @@
|
||||
<dependency>
|
||||
<groupId>org.openjfx</groupId>
|
||||
<artifactId>javafx-controls</artifactId>
|
||||
<version>11</version>
|
||||
<version>${javafx.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjfx</groupId>
|
||||
<artifactId>javafx-fxml</artifactId>
|
||||
<version>11</version>
|
||||
<version>${javafx.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<!-- JavaFX Maven Plugin -->
|
||||
<plugin>
|
||||
<groupId>org.openjfx</groupId>
|
||||
<artifactId>javafx-maven-plugin</artifactId>
|
||||
<version>0.0.7</version>
|
||||
<configuration>
|
||||
<mainClass>cn.nightsoil.liveaudio.LiveAudioApplication</mainClass>
|
||||
<mainClass>${exec.mainClass}</mainClass>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<!-- Spring Boot Maven Plugin -->
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
|
||||
<!-- Maven Compiler Plugin -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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<String> 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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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/
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
<Accordion layoutX="126.0" layoutY="75.0" />
|
||||
<Label layoutX="155.0" layoutY="96.0" prefHeight="34.0" prefWidth="64.0" text="音频目录:" />
|
||||
<TextField fx:id="audioPathText" 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="播放" />
|
||||
<Button fx:id="selectAudioFolder" layoutX="393.0" layoutY="102.0" mnemonicParsing="false" onAction="#getAudioFolder" text="选择文件夹" />
|
||||
<Button fx:id="playButton" layoutX="155.0" layoutY="260.0" mnemonicParsing="false" onAction="#togglePlayback" text="播放" />
|
||||
</children>
|
||||
</AnchorPane>
|
||||
|
||||
Reference in New Issue
Block a user