java - 创建 JavaFX Video Player 类的实例

标签 java javafx

对 Java 相当陌生,正在尝试自学一些有关 JavaFX 的知识。我正在尝试创建一个简单的 JavaFX 视频/媒体播放器,该播放器在单击视频文件时运行。 我想将实际的播放器创建为一个单独的类,该类接受视频文件位置作为字符串参数。

当我运行以下命令时,

public class BLPlayer{
    public static void main(String[] args) {
        if(args.length > 0){
            VideoPlayer vp = new VideoPlayer(args);
        }else{
            //showGUI();
        }
    }
}


public class VideoPlayer extends Application {
    String path;
    MediaPlayer player;
    Scene scene;
    MediaView view;
    Group root;
    Media media;

    VideoPlayer(String[] args){
        path = args[0];
        path = path.replace("\\", "/"); 
        launch(args);
    }

    @Override
    public void start(final Stage stage) throws Exception {

        File f = new File(path);

        root = new Group();
        media = new Media(f.toURI().toString());
        player = new MediaPlayer(media);
        view = new MediaView(player);
        root.getChildren().add(view);
        scene = new Scene(root, 400, 400, Color.BLACK);
        stage.setScene(scene);
        stage.show();
        player.play();
    }

}

我收到错误:

Exception in Application constructor
Exception in thread "main" java.lang.RuntimeException: Unable to construct Application instance: class player.VideoPlayer
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:884)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:56)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:158)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoSuchMethodException: player.VideoPlayer.<init>()
at java.lang.Class.getConstructor0(Class.java:2971)
at java.lang.Class.getConstructor(Class.java:1812)
at com.sun.javafx.application.LauncherImpl$7.run(LauncherImpl.java:790)
at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:335)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:298)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)

我不知道为什么它不起作用。我将不胜感激任何提示或建议!感谢您的帮助。

最佳答案

发生错误的原因是您需要使用launch()启动JavaFX应用程序。了解更多详情go through this solution .

如果你确实需要向VideoPlayer类发送参数。您可以使用 Application 类的 getParameters() 获取参数。

public class VideoPlayer extends Application {
    String path;
    MediaPlayer player;
    Scene scene;
    MediaView view;
    Group root;
    Media media;

    @Override
    public void start(final Stage stage) throws Exception {
        Parameters params = getParameters();
        final List<String> parameters = params.getRaw();
        path = !parameters.isEmpty() ? parameters.get(0) : "";
        path = path.replace("\\", "/"); 
        root = new Group();
        File f = new File(path);
        root = new Group();
        media = new Media(f.toURI().toString());
        player = new MediaPlayer(media);
        view = new MediaView(player);
        root.getChildren().add(view);
        scene = new Scene(root, 400, 400, Color.BLACK);
        stage.setScene(scene);
        stage.show();
        player.play();
    }
}

从另一个类启动 JavaFX 应用程序

public class BLPlayer {
    public static void main(String[] args) {
        if(args.length > 0){
            Application.launch(VideoPlayer.class, args);
        }
    }
}

关于java - 创建 JavaFX Video Player 类的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25341533/

相关文章:

multithreading - JavaFX 从 Controller(Singleton) 访问 ui 元素

java - SimpleDateFormat android 没有按预期格式化

java - Jboss EAP 7 - 如何从部署中排除隐式模块 (javax.jms)?

java - getPurchasesList 空指针异常

java - 如何从另一个模块访问 Spring Boot Quartz Bean?

javafx - ComboBox 的 setButtonCell

java - 如何在 javafx 中创建一个弹出窗口

java - 创建一个简单的自定义 ScrollView

java - 每个窗口只打开一次

java - Java 中 if/else 的缩写形式