如何在 java FX 中加载视频

How to load video in java FX

我想将视频插入到 javaFX 中。从计算机插入视频 - 不是来自 youtube 或其他东西。 Play/pause/minimize 不需要按钮和边框。

import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;

public class MediaMP4 extends Application {
    Stage window;
    Scene scene1;


    public static void main(String[] args) {
    launch(args);
}
@Override
public void start(Stage primaryStage) throws FileNotFoundException   {

    window = primaryStage;
    primaryStage.setTitle("Moves");


    Media media = new Media ("pr.mp4");
    MediaPlayer player = new MediaPlayer (media);
    MediaView view = new MediaView (player);


    Group full = new Group ();
    full.getChildren().addAll(view);


    scene1 = new Scene (full,600,600);
    primaryStage.setScene(scene1);
    window.show();

    player.play();
    }
}

我的 pr.mp4 文件在项目中,不在包中。

堆栈跟踪:

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication5(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: uri.getScheme() == null! uri == 'pr.mp4'
at com.sun.media.jfxmedia.locator.Locator.<init>(Locator.java:211)
at javafx.scene.media.Media.<init>(Media.java:393)
at vv.MediaMP4.start(MediaMP4.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication12(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater4(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null8(WinApplication.java:191)
... 1 more
Exception running application vv.MediaMP4

堆栈跟踪中的相关行是:

Caused by: java.lang.IllegalArgumentException: uri.getScheme() == null! uri == 'pr.mp4'.

您需要指定一个带有方案的 URI 字符串作为构造函数参数,如指定的那样 here

所以把你的台词改成这样:

Media media = new Media ("pr.mp4");

像这样:

Media media = new Media("file://c:/myproject/pr.mp4"));

查看此问题了解更多详情:How to target a file (a path to it) in Java/JavaFX