jdk17使用java写一个多个MP3合成一个 的代码
时间: 2024-10-14 17:02:37 浏览: 26
多个jar包合并成一个jar包的办法
在Java中,使用JDK17和JavaFX库可以创建一个简单的音频文件合并工具。这里是一个基本的示例,它使用了`javafx.scene.media.MediaPlayer`来播放MP3文件并进行合并:
```java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
import java.io.File;
import java.util.ArrayList;
public class MP3Merger extends Application {
private ArrayList<MediaPlayer> mediaPlayerList = new ArrayList<>();
private int currentTrackIndex = 0;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Button playButton = new Button("Play");
playButton.setOnAction(event -> {
if (mediaPlayerList.get(currentTrackIndex).getStatus() != MediaPlayer.Status.STOPPED) {
mediaPlayerList.get(currentTrackIndex).pause();
} else {
mediaPlayerList.get(currentTrackIndex).play();
}
});
StackPane root = new StackPane();
root.getChildren().add(playButton);
Scene scene = new Scene(root, 300, 250);
File mp31 = new File("path_to_your_first_mp3.mp3"); // 替换为第一个MP3路径
Media media1 = new Media(mp31.toURI().toString());
mediaPlayerList.add(new MediaPlayer(media1));
// 添加更多MP3,按照这种方式添加到列表
// ...
primaryStage.setScene(scene);
primaryStage.show();
}
public void addTrack(File mp3File) {
Media media = new Media(mp3File.toURI().toString());
mediaPlayerList.add(new MediaPlayer(media));
}
public void stopAllTracks() {
for (MediaPlayer mediaPlayer : mediaPlayerList) {
mediaPlayer.stop();
}
mediaPlayerList.clear();
currentTrackIndex = 0;
}
public void nextTrack() {
if (currentTrackIndex < mediaPlayerList.size() - 1) {
currentTrackIndex++;
} else {
System.out.println("No more tracks to play.");
}
}
public void previousTrack() {
if (currentTrackIndex > 0) {
currentTrackIndex--;
} else {
System.out.println("Already at the first track.");
}
}
}
阅读全文