java - 在普通 Java 应用程序中使用 JavaFX MediaPlayer 播放音频?

标签 java audio javafx-2

我需要能够在普通的 Java 项目中播放音频文件 (MP3/Wav)。我更喜欢使用新的 JavaFX MediaPlayer 而不是 JMF。我写了一些代码来测试这个:

public void play()
{
    URL thing = getClass().getResource("mysound.wav");
    Media audioFile = new Media( thing.toString() );     
    try
    {                                       
        MediaPlayer player = new MediaPlayer(audioFile);
        player.play();
    }
    catch (Exception e)
    {
        System.out.println( e.getMessage() );
        System.exit(0);
    }        
}

当我运行它时,出现异常:Toolkit 未初始化

我知道这与 JavaFX 线程有关。我的问题是,我该如何解决这个问题?我是否需要创建一个 JavaFX 面板只是为了在我的普通应用程序的后台播放一些音频文件,还是有任何其他方式?

编辑:堆栈跟踪:

java.lang.IllegalStateException: Toolkit not initialized
    at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:121)
    at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:116)
    at javafx.application.Platform.runLater(Platform.java:52)
    at javafx.scene.media.MediaPlayer.init(MediaPlayer.java:445)
    at javafx.scene.media.MediaPlayer.<init>(MediaPlayer.java:360)
    at javaapplication6.JavaApplication6.play(JavaApplication6.java:23)
    at javaapplication6.JavaApplication6.main(JavaApplication6.java:14)

最佳答案

对于在 Swing 中集成 JavaFX MediaPlayer 的解决方案

使用 JFXPanel并注意仅在 JavaFX 线程上和 JavaFX 系统正确初始化后使用 JavaFX 对象。

JavaFX 是普通的 Java,这让问题有点困惑,但我猜你指的是 Swing。

这是一个从 Swing 启动的示例音频播放器。该示例假设在 Windows 7 的默认公共(public)示例音乐文件夹 (C:\Users\Public\Music\Sample Music) 中有一堆 mp3 文件,并依次播放每个文件。

app screenshot

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 的 native JavaFX 应用程序

上面使用 Swing 的解决方案回答了所提出的问题。但是,我注意到有时人们会选择这个答案并使用它来创建基于 Java 的媒体播放器,即使他们没有要将其应用程序嵌入其中的现有 Swing 应用程序也是如此。

如果您没有现有的 Swing 应用程序,请从您的应用程序中完全删除 Swing 代码并编写一个 native JavaFX 应用程序。为此,请使用下面的 JavaFXMediaPlayer 类,而不是上面示例中的 JavaFXMediaPlayerLaunchedFromSwing 类。

JavaFXMediaPlayer

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();
    }
}

后续问题的答案

Will my .JAR file built via swing automatically have JavaFX added to it once I add it to the libraries in netbeans?

注意:此后续答复中有关包装的信息现在可能已过时,此时存在其他首选包装选项(例如 https://github.com/openjfx/javafx-maven-plugin)。

从技术上讲,Swing 不会构建 Jar 文件,但 javafx 打包命令的 jar 可以。

如果你的应用包含JavaFX,那么,最好use the JavaFX packaging tools .没有它们,您可能会遇到一些部署问题,因为 Java 运行时 jar (jfxrt.jar) 不会自动出现在 jdk7u7 的 java 引导类路径中。用户可以手动将其添加到他们的运行时类路径中,但这可能会有点麻烦。在未来的 jdk 版本中(可能是 jdk7u10 或 jdk8),jfxrt.jar 将位于类路径中。即使那样,仍然建议使用 JavaFX 打包工具,因为它是确保您的部署包以最兼容的方式工作的最佳方式。

SwingInterop NetBeans 项目是一个示例 NetBeans 项目,它将 JavaFX 部署工具用于嵌入 JavaFX 组件的 Swing 项目。 SwingInterop 的源代码是 JDK 7 and JavaFX Demos and Samples 的一部分下载。

关于java - 在普通 Java 应用程序中使用 JavaFX MediaPlayer 播放音频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12548603/

相关文章:

java - 嵌套的 JSON 对象反序列化为 JSONObject

audio - 采样率可靠吗?

javafx-2 - : FXML Specification? 在哪里

javafx-2 - 使用JavaFX检查形状碰撞

java - 如何在 Java 中解压缩目录中的所有 Zip 文件夹?

java - 是否可以使用微基准框架只对某些语句计时?

java - 如何循环整个数组并获取它?

objective-c - 多线程从何而来?

android - 用户在默认录音机中点击停止后返回 Activity

java - 在 updateProgress 期间将文本附加到 JavaFX TextArea