创建JavaFX视频播放器类的实例
对Java相当陌生,并试图教自己一些JavaFX。我正在尝试创建一个简单的JavaFX视频/媒体播放器,当我单击一个视频文件时它就会运行。 我想将实际的播放器创建为一个单独的类,该类接受视频文件位置作为字符串参数 当我运行下面的创建JavaFX视频播放器类的实例,java,javafx,Java,Javafx,对Java相当陌生,并试图教自己一些JavaFX。我正在尝试创建一个简单的JavaFX视频/媒体播放器,当我单击一个视频文件时它就会运行。 我想将实际的播放器创建为一个单独的类,该类接受视频文件位置作为字符串参数 当我运行下面的 public class BLPlayer{ public static void main(String[] args) { if(args.length > 0){ VideoPlayer vp = new Vi
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)
应用程序构造函数中的异常
线程“main”java.lang.RuntimeException中出现异常:无法构造应用程序实例:class player.VideoPlayer
位于com.sun.javafx.application.LaunchImpl.launchApplication1(LaunchImpl.java:884)
在com.sun.javafx.application.launchempl.access$000(launchempl.java:56)
位于com.sun.javafx.application.launchempl$1.run(launchempl.java:158)
运行(Thread.java:745)
原因:java.lang.NoSuchMethodException:player.VideoPlayer。()
位于java.lang.Class.getConstructor0(Class.java:2971)
位于java.lang.Class.getConstructor(Class.java:1812)
位于com.sun.javafx.application.launchempl$7.run(launchempl.java:790)
位于com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:335)
位于com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301)
位于com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298)
位于java.security.AccessController.doPrivileged(本机方法)
位于com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:298)
位于com.sun.glass.ui.invokelateDispatcher$Future.run(invokelateDispatcher.java:95)
在com.sun.glass.ui.win.WinApplication.\u runLoop(本机方法)
com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
位于com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
我不知道为什么它不起作用。如果有任何提示或建议,我将不胜感激!感谢您的帮助。发生此错误是因为您需要使用
launch()
启动JavaFX应用程序。更多细节
如果您确实需要向VideoPlayer类发送参数。您可以使用应用程序
类的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();
}
}
public class BLPlayer {
public static void main(String[] args) {
if(args.length > 0){
Application.launch(VideoPlayer.class, args);
}
}
}