加载视频文件出现错误

项目场景:

在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"
        },
相关推荐
Resean02236 小时前
架构设计系列(十六):DDD 与代码架构
java·开发语言·架构
学长毕设7 小时前
基于SpringBoot 车辆租赁系统平台-附源码
java·spring boot·后端
Flynt9 小时前
Spring AI Alibaba 真停更了?我把它的 GitHub 翻了个底朝天
java·spring·开源
她的男孩12 小时前
Spring Boot 接 Flowable:请假单要同时走财务和行政,包容分支和默认分支怎么配
java·spring boot·工作流引擎
咖啡八杯13 小时前
LoginUser 适配器模式:适配 SysUser 到 UserDetails
java·开发语言·后端·框架·适配器模式·若依
北冥有鱼14 小时前
从 bean::method 里抠出注解:Java 方法引用元数据反查全记录
java·后端
CoderYanger14 小时前
Java EE 进阶:11. SpringBoot 统一功能处理(第四弹):@ControllerAdvice 源码分析+案例代码
java·数据库·spring boot·后端·spring·java-ee·mybatis
玖米GEO运营王总监18 小时前
抖音和视频号算法区别:玖米快跑的双端分发信号与内容适配对比
java·前端·人工智能
AI人工智能+电脑小能手19 小时前
集合按大小切片分批,这个Java工具类5个方法含基本类型数组
java·工具类·批量处理·集合分片·分批调用
SimonKing19 小时前
高星红果短剧的TV版|桌面版都在这里了
java·后端·程序员