发布于2021-11-21 18:50 阅读(1348) 评论(0) 点赞(24) 收藏(3)
问题:无法在我的JavaFX项目中导入MediaPlayer。将IntelliJ与Maven,JDK 17一起使用。即便更新了Maven依赖项,安装了JavaFX插件,但它仍然不起作用。
解决方法:使用maven导入java.media包而非手动导入/删除手动导入jar包
1. module-info.java中增添media 引用
- module com.example.javafx {
- requires javafx.controls;
- requires javafx.fxml;
- requires javafx.media;
-
-
- opens com.example.javafx to javafx.fxml;
- exports com.example.javafx;
- }
2. 打开项目结构-库-检查引用包
1. 请检查javafx-media包版本是否是17.0.0.1 第一次maven加载包会自动加载11.0.2的包,可能因为版本不匹配,导致不能正常导入
2. 请检查本地导入的javafx包是否与maven自动导入的包相冲突,如果是,请删除本地导入的包
3. 打开pom.xml 检查依赖是否正确
-
-
- <dependencies>
- <dependency>
- <groupId>org.openjfx</groupId>
- <artifactId>javafx-controls</artifactId>
- <version>17.0.0.1</version>
- </dependency>
- <dependency>
- <groupId>org.openjfx</groupId>
- <artifactId>javafx-media</artifactId>//需要导入javafx-media依赖
- <version>17.0.0.1</version>//版本号为17.0.0.1
- </dependency>
- <dependency>
- <groupId>org.openjfx</groupId>
- <artifactId>javafx-fxml</artifactId>
- <version>17.0.0.1</version>
- </dependency>
-
- <dependency>
- <groupId>org.junit.jupiter</groupId>
- <artifactId>junit-jupiter-api</artifactId>
- <version>${junit.version}</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.junit.jupiter</groupId>
- <artifactId>junit-jupiter-engine</artifactId>
- <version>${junit.version}</version>
- <scope>test</scope>
- </dependency>
-
- </dependencies>
-
4. 最后如果导入没有问题,但是类似这种类型报错:
例:在一个媒体视图中播放视频
- public class App15_17 extends Application{
- // String eURL="http://www.gov.cn/guoqing/guoge/hc.mp3";
- String eURL="file:///D:java源代码2021秋/chap15/music";
- @Override
- public void start(Stage stage){
- Media media=new Media(eURL);
- MediaPlayer mPlayer=new MediaPlayer(media);
- MediaView mView=new MediaView(mPlayer);
- mView.setFitWidth(800);
- mView.setFitHeight(600);
-
- Button pBut=new Button(">");
- pBut.setOnAction(e->{
- if(pBut.getText().equals(">")){
- mPlayer.play();
- pBut.setText("||");
- }else{
- mPlayer.pause();
- pBut.setText(">");
- }
- });
-
- Button rBut=new Button("<<");
- rBut.setOnAction(e->mPlayer.seek(Duration.ZERO));
- Slider sVol=new Slider();
- sVol.setMinWidth(30);
- sVol.setPrefWidth(150);
- sVol.setValue(50);
- mPlayer.volumeProperty().bind(sVol.valueProperty().divide(100));
- HBox hB=new HBox(10);
- hB.setAlignment(Pos.CENTER);
- Label vol=new Label("音量");
- hB.getChildren().addAll(pBut,rBut,vol,sVol);
- BorderPane bPane=new BorderPane();
- bPane.setCenter(mView);
- bPane.setBottom(hB);
- Scene scene=new Scene(bPane);
- stage.setTitle("视频播放器");
- stage.setScene(scene);
- stage.show();
- }
- }
报错:
Exception in Application start method
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: MediaException: MEDIA_UNSUPPORTED : Unrecognized file signature!
at javafx.media/javafx.scene.media.Media.<init>(Media.java:411)
at com.example.javafx/com.example.javafx.App15_17.start(App15_17.java:19)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
... 1 more
请检查媒体对象构造方法的媒体源路径,即
String eURL="file:///D:java源代码2021秋/chap15/music";是否正确,此案例中将eURL改
String eURL="http://www.gov.cn/guoqing/guoge/hc.mp3";即可正常运行,如果确认eURL正确,仍然报错,请重新返回第一步进行检查
原文链接:https://blog.csdn.net/q1248940057/article/details/121426463
作者:helloworld
链接:http://www.javaheidong.com/blog/article/326235/cd253f6d066eeb107801/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!