问题:无法在我的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 检查依赖是否正确
org.openjfx
javafx-controls
17.0.0.1
org.openjfx
javafx-media//需要导入javafx-media依赖
17.0.0.1 //版本号为17.0.0.1
org.openjfx
javafx-fxml
17.0.0.1
org.junit.jupiter
junit-jupiter-api
${junit.version}
test
org.junit.jupiter
junit-jupiter-engine
${junit.version}
test
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.
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正确,仍然报错,请重新返回第一步进行检查



