加载视频文件出现错误

项目场景:

在javafx框架下,在使用Image类通过URL加载视频的时候,出现加载错误。


问题描述

Media media = new Media(URL_MEDIA);

MediaPlayer mediaPlayer = new MediaPlayer(media);

MediaView mediaView = new MediaView(mediaPlayer);

以上代码引起的如下报错:

cpp 复制代码
Exception in Application start method
java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:118)
        at java.base/java.lang.reflect.Method.invoke(Method.java:580)
        at javafx.graphics@22.0.1/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)        
        at javafx.graphics@22.0.1/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
        at java.base/java.lang.reflect.Method.invoke(Method.java:580)
        at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1147)
Caused by: java.lang.RuntimeException: Exception in Application start method
        at javafx.graphics@22.0.1/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:893)
        at javafx.graphics@22.0.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)       
        at java.base/java.lang.Thread.run(Thread.java:1570)
Caused by: java.lang.IllegalAccessError: class com.sun.media.jfxmedia.locator.Locator (in unnamed module @0x188b3d43) cannot access class com.sun.javafx.PlatformUtil (in module javafx.base) because module javafx.base does not export com.sun.javafx to unnamed module @0x188b3d43
        at com.sun.media.jfxmedia.locator.Locator.<init>(Locator.java:235)
        at javafx.scene.media.Media.<init>(Media.java:392)
        at MyMedia.start(MyMedia.java:25)
        at javafx.graphics@22.0.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:839)      
        at javafx.graphics@22.0.1/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:483)
        at javafx.graphics@22.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:456)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
        at javafx.graphics@22.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:455)
        at javafx.graphics@22.0.1/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at javafx.graphics@22.0.1/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at javafx.graphics@22.0.1/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
        ... 1 more
Exception running application MyMedia

原因分析:

开始以为是路径原因造成的无法找到视频文件,所以加了throw exception来看,但是发现不是这个问题,是缺少javafx.media造成的无法引用media模块。


解决方案:

最终在launch.json中加上了下属的media模块

cpp 复制代码
    "version": "0.2.0",
    "configurations": [
        {
            "type": "java",
            "name": "MyMedia",
            "request": "launch",
            "mainClass": "MyMedia",
            "projectName": "java_d2184a8b",
            "vmArgs": "--module-path lib/ --add-modules=javafx.controls,javafx.fxml,javafx.media"
        },
相关推荐
无糖冰可乐2117 分钟前
IDEA多java版本切换
java·ide·intellij-idea
合作小小程序员小小店23 分钟前
web开发,在线%超市销售%管理系统,基于idea,html,jsp,java,ssh,sql server数据库。
java·前端·sqlserver·ssh·intellij-idea
brucelee18624 分钟前
IntelliJ IDEA 设置 Local History 永久保留
java·ide·intellij-idea
Pluto_CSND2 小时前
Java中的静态代理与动态代理(Proxy.newProxyInstance)
java·开发语言
百***46453 小时前
Java进阶-在Ubuntu上部署SpringBoot应用
java·spring boot·ubuntu
serve the people3 小时前
Prompts for Chat Models in LangChain
java·linux·langchain
一叶飘零_sweeeet4 小时前
不止于 API 调用:解锁 Java 工具类设计的三重境界 —— 可复用性、线程安全与性能优化
java·工具类
A阳俊yi5 小时前
Spring Data JPA
java·开发语言
小王不爱笑1326 小时前
Spring AOP(AOP+JDBC 模板 + 转账案例)
java·后端·spring
遇印记6 小时前
蓝桥java蜗牛
java·学习·蓝桥杯