对于在 Swing 中集成 JavaFX MediaPlayer 的解决方案
使用JFXPanel并注意仅在 JavaFX 线程上和 JavaFX 系统正确初始化后才使用 JavaFX 对象。
JavaFX 是普通的 Java,这使问题有点混乱,但我猜你的意思是 Swing。
这是从 Swing 启动的示例音频播放器。该示例假定 Windows 7 的默认公共示例音乐文件夹 (C:\Users\Public\Music\Sample Music) 中有一堆 mp3 文件,并依次播放每个文件。
JavaFXMediaPlayerLaunchedFromSwing.java
此代码负责创建一个 Swing 应用程序,该应用程序反过来初始化 JavaFX 工具包并在 JavaFX 应用程序线程上创建一个 JavaFX 场景。
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javax.swing.*;
/**
* Example of playing all mp3 audio files in a given directory
* using a JavaFX MediaView launched from Swing
*/
public class JavaFXMediaPlayerLaunchedFromSwing {
private static void initAndShowGUI() {
// This method is invoked on Swing thread
JFrame frame = new JFrame("FX");
final JFXPanel fxPanel = new JFXPanel();
frame.add(fxPanel);
frame.setBounds(200, 100, 800, 250);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
Platform.runLater(() -> initFX(fxPanel));
}
private static void initFX(JFXPanel fxPanel) {
// This method is invoked on JavaFX thread
Scene scene = new MediaSceneGenerator().createScene();
fxPanel.setScene(scene);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(
JavaFXMediaPlayerLaunchedFromSwing::initAndShowGUI
);
}
}
MediaSceneGenerator.java
创建一个 JavaFX 媒体播放器,它按顺序播放.mp3
给定文件夹中的所有媒体文件。它为媒体提供了一些控制(播放、暂停、跳过曲目、当前曲目播放进度指示器)。
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.layout.VBox;
import javafx.scene.media.*;
import javafx.util.Duration;
import java.io.File;
import java.util.*;
public class MediaSceneGenerator {
private static final String MUSIC_FOLDER = "C:\\Users\\Public\\Music\\Sample Music";
private static final String MUSIC_FILE_EXTENSION = ".mp3";
private final Label currentlyPlaying = new Label();
private final ProgressBar progress = new ProgressBar();
private ChangeListener<Duration> progressChangeListener;
public Scene createScene() {
final StackPane layout = new StackPane();
// determine the source directory for the playlist
final File dir = new File(MUSIC_FOLDER);
if (!dir.exists() || !dir.isDirectory()) {
System.out.println("Cannot find media source directory: " + dir);
Platform.exit();
return null;
}
// create some media players.
final List<MediaPlayer> players = new ArrayList<>();
for (String file : Objects.requireNonNull(dir.list((dir1, name) -> name.endsWith(MUSIC_FILE_EXTENSION))))
players.add(
createPlayer(
normalizeFileURL(dir, file)
)
);
if (players.isEmpty()) {
System.out.println("No audio found in " + dir);
Platform.exit();
return null;
}
// create a view to show the mediaplayers.
final MediaView mediaView = new MediaView(players.get(0));
final Button skip = new Button("Skip");
final Button play = new Button("Pause");
// play each audio file in turn.
for (int i = 0; i < players.size(); i++) {
MediaPlayer player = players.get(i);
MediaPlayer nextPlayer = players.get((i + 1) % players.size());
player.setOnEndOfMedia(() -> {
final MediaPlayer curPlayer = mediaView.getMediaPlayer();
nextPlayer.seek(Duration.ZERO);
if (nextPlayer != curPlayer) {
curPlayer.currentTimeProperty().removeListener(progressChangeListener);
}
mediaView.setMediaPlayer(nextPlayer);
nextPlayer.play();
});
}
// allow the user to skip a track.
skip.setOnAction(actionEvent -> {
final MediaPlayer curPlayer = mediaView.getMediaPlayer();
MediaPlayer nextPlayer = players.get((players.indexOf(curPlayer) + 1) % players.size());
nextPlayer.seek(Duration.ZERO);
mediaView.setMediaPlayer(nextPlayer);
if (nextPlayer != curPlayer) {
curPlayer.currentTimeProperty().removeListener(progressChangeListener);
}
nextPlayer.play();
});
// allow the user to play or pause a track.
play.setOnAction(actionEvent -> {
if ("Pause".equals(play.getText())) {
mediaView.getMediaPlayer().pause();
play.setText("Play");
} else {
mediaView.getMediaPlayer().play();
play.setText("Pause");
}
});
// display the name of the currently playing track.
mediaView.mediaPlayerProperty().addListener(
(observableValue, oldPlayer, newPlayer) -> setCurrentlyPlaying(newPlayer)
);
// start playing the first track.
mediaView.setMediaPlayer(players.get(0));
mediaView.getMediaPlayer().play();
setCurrentlyPlaying(mediaView.getMediaPlayer());
// silly invisible button used as a template to get the actual preferred size of the Pause button.
Button invisiblePause = new Button("Pause");
invisiblePause.setVisible(false);
play.prefHeightProperty().bind(invisiblePause.heightProperty());
play.prefWidthProperty().bind(invisiblePause.widthProperty());
// layout the scene.
HBox controls = new HBox(10, skip, play, progress);
controls.setAlignment(Pos.CENTER);
VBox mediaPanel = new VBox(10, currentlyPlaying, mediaView, controls);
layout.setStyle("-fx-background-color: cornsilk; -fx-font-size: 20; -fx-padding: 20; -fx-alignment: center;");
layout.getChildren().addAll(
invisiblePause,
mediaPanel
);
progress.setMaxWidth(Double.MAX_VALUE);
HBox.setHgrow(progress, Priority.ALWAYS);
return new Scene(layout);
}
/**
* sets the currently playing label to the label of the new media player and updates the progress monitor.
*/
private void setCurrentlyPlaying(final MediaPlayer newPlayer) {
progress.setProgress(0);
progressChangeListener = (observableValue, oldValue, newValue) ->
progress.setProgress(
1.0 * newPlayer.getCurrentTime().toMillis() / newPlayer.getTotalDuration().toMillis()
);
newPlayer.currentTimeProperty().addListener(progressChangeListener);
String source = getUserFriendlyMediaName(newPlayer);
currentlyPlaying.setText("Now Playing: " + source);
}
/**
* @return a MediaPlayer for the given source which will report any errors it encounters
*/
private MediaPlayer createPlayer(String aMediaSrc) {
System.out.println("Creating player for: " + aMediaSrc);
final MediaPlayer player = new MediaPlayer(new Media(aMediaSrc));
player.setOnError(() -> System.out.println("Media error occurred: " + player.getError()));
return player;
}
private String normalizeFileURL(File dir, String file) {
return "file:///" + (dir + "\\" + file).replace("\\", "/").replaceAll(" ", "%20");
}
private String getUserFriendlyMediaName(MediaPlayer newPlayer) {
String source = newPlayer.getMedia().getSource();
source = source.substring(0, source.length() - MUSIC_FILE_EXTENSION.length());
source = source.substring(source.lastIndexOf("/") + 1).replaceAll("%20", " ");
return source;
}
}
如果您只想要一个带有 MediaPlayer 而没有 Swing 的本机 JavaFX 应用程序
上面使用 Swing 的解决方案回答了所提出的问题。但是,我注意到有时人们已经选择了这个答案并使用它来创建基于 Java 的媒体播放器,即使他们没有将应用程序嵌入其中的现有 Swing 应用程序。
如果您没有现有的 Swing 应用程序,则从您的应用程序中完全消除 Swing 代码,并改为编写本机 JavaFX 应用程序。为此,请使用JavaFXMediaPlayer
下面的类而不是JavaFXMediaPlayerLaunchedFromSwing
上面示例中的类。
JavaFX媒体播放器
import javafx.application.Application;
import javafx.stage.Stage;
public class JavaFXMediaPlayer extends Application {
@Override
public void start(Stage stage) throws Exception {
stage.setScene(new MediaSceneGenerator().createScene());
stage.show();
}
}
后续问题的答案
一旦我将通过 swing 构建的 .JAR 文件添加到 netbeans 中的库,它会自动添加 JavaFX 吗?
注意:此关于包装的后续答案中的信息现在可能已过时,此时存在其他首选包装选项(例如https://github.com/openjfx/javafx-maven-plugin)。
从技术上讲,Swing 不会构建 Jar 文件,但 javafx 打包命令的 jar 会。
如果您的应用程序包含 JavaFX,那么最好使用 JavaFX 打包工具。如果没有它们,您可能会遇到一些部署问题,因为 Java 运行时 jar (jfxrt.jar) 不会自动出现在 jdk7u7 的 java 引导类路径中。用户可以手动将其添加到他们的运行时类路径中,但这可能有点麻烦。在未来的 jdk 版本(可能是 jdk7u10 或 jdk8)中,jfxrt.jar 将位于类路径中。即便如此,仍然建议使用 JavaFX 打包工具,因为这将是确保您的部署包以最兼容的方式工作的最佳方式。
SwingInterop NetBeans 项目是一个示例NetBeans 项目,它利用 JavaFX 部署工具来嵌入 JavaFX 组件的 Swing 项目。SwingInterop 的源代码是JDK 7 和 JavaFX Demos and Samples下载的一部分。