程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-06(4)

IDEA &JDK17.0 JAVAFX 17.0 java.media 导入不正确

发布于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 引用

  1. module com.example.javafx {
  2. requires javafx.controls;
  3. requires javafx.fxml;
  4. requires javafx.media;
  5. opens com.example.javafx to javafx.fxml;
  6. exports com.example.javafx;
  7. }

 2. 打开项目结构-库-检查引用包

        1. 请检查javafx-media包版本是否是17.0.0.1 第一次maven加载包会自动加载11.0.2的包,可能因为版本不匹配,导致不能正常导入

        2. 请检查本地导入的javafx包是否与maven自动导入的包相冲突,如果是,请删除本地导入的包

3. 打开pom.xml 检查依赖是否正确

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.openjfx</groupId>
  4. <artifactId>javafx-controls</artifactId>
  5. <version>17.0.0.1</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.openjfx</groupId>
  9. <artifactId>javafx-media</artifactId>//需要导入javafx-media依赖
  10. <version>17.0.0.1</version>//版本号为17.0.0.1
  11. </dependency>
  12. <dependency>
  13. <groupId>org.openjfx</groupId>
  14. <artifactId>javafx-fxml</artifactId>
  15. <version>17.0.0.1</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.junit.jupiter</groupId>
  19. <artifactId>junit-jupiter-api</artifactId>
  20. <version>${junit.version}</version>
  21. <scope>test</scope>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.junit.jupiter</groupId>
  25. <artifactId>junit-jupiter-engine</artifactId>
  26. <version>${junit.version}</version>
  27. <scope>test</scope>
  28. </dependency>
  29. </dependencies>

4. 最后如果导入没有问题,但是类似这种类型报错:

例:在一个媒体视图中播放视频

  1. public class App15_17 extends Application{
  2. // String eURL="http://www.gov.cn/guoqing/guoge/hc.mp3";
  3. String eURL="file:///D:java源代码2021秋/chap15/music";
  4. @Override
  5. public void start(Stage stage){
  6. Media media=new Media(eURL);
  7. MediaPlayer mPlayer=new MediaPlayer(media);
  8. MediaView mView=new MediaView(mPlayer);
  9. mView.setFitWidth(800);
  10. mView.setFitHeight(600);
  11. Button pBut=new Button(">");
  12. pBut.setOnAction(e->{
  13. if(pBut.getText().equals(">")){
  14. mPlayer.play();
  15. pBut.setText("||");
  16. }else{
  17. mPlayer.pause();
  18. pBut.setText(">");
  19. }
  20. });
  21. Button rBut=new Button("<<");
  22. rBut.setOnAction(e->mPlayer.seek(Duration.ZERO));
  23. Slider sVol=new Slider();
  24. sVol.setMinWidth(30);
  25. sVol.setPrefWidth(150);
  26. sVol.setValue(50);
  27. mPlayer.volumeProperty().bind(sVol.valueProperty().divide(100));
  28. HBox hB=new HBox(10);
  29. hB.setAlignment(Pos.CENTER);
  30. Label vol=new Label("音量");
  31. hB.getChildren().addAll(pBut,rBut,vol,sVol);
  32. BorderPane bPane=new BorderPane();
  33. bPane.setCenter(mView);
  34. bPane.setBottom(hB);
  35. Scene scene=new Scene(bPane);
  36. stage.setTitle("视频播放器");
  37. stage.setScene(scene);
  38. stage.show();
  39. }
  40. }

报错:

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黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

24 0
收藏该文
已收藏

评论内容:(最多支持255个字符)