gpt4 book ai didi

java - 在 JavaFX 中创建媒体播放器

转载 作者:行者123 更新时间:2023-12-01 11:20:42 25 4
gpt4 key购买 nike

我正在尝试在 JavaFX 中创建媒体播放器,但在运行时遇到错误。我的类(class)如下:

Main.java

package application;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;


public class Main extends Application {
@Override
public void start(Stage primaryStage) {
Player player = new Player("file:///C:\Users\Momin\Downloads\Lesson 1A- Building Layouts Videos\01 - Introduction");
Scene scene = new Scene(player, 852, 480, Color.BLACK);
primaryStage.setScene(scene);
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}

Player.java

package application;

import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;

public class Player extends BorderPane {

Media media;
MediaPlayer player;
MediaView view;
Pane mpane;

public Player(String file) {
media = new Media(file);
player = new MediaPlayer(media);
view = new MediaView(player);
mpane = new Pane();

mpane.getChildren().add(view);

setCenter(mpane);

player.play();
}

}

当我运行 Main.java 时,出现以下错误:

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(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
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(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$50/1323468230.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: java.net.URISyntaxException: Illegal character in path at index 35: file:///C:/Users/Momin/Desktop/Java Projects/Player/Videos.sample.mp4
at javafx.scene.media.Media.<init>(Unknown Source)
at application.Player.<init>(Player.java:17)
at application.Main.start(Main.java:13)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$53/192199497.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/186276003.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/29678400.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/237061348.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
... 1 more
Caused by: java.net.URISyntaxException: Illegal character in path at index 35: file:///C:/Users/Momin/Desktop/Java Projects/Player/Videos.sample.mp4
at java.net.URI$Parser.fail(Unknown Source)
at java.net.URI$Parser.checkChars(Unknown Source)
at java.net.URI$Parser.parseHierarchical(Unknown Source)
at java.net.URI$Parser.parse(Unknown Source)
at java.net.URI.<init>(Unknown Source)
... 17 more
Exception running application application.Main

请帮忙。谢谢。

最佳答案

使用"file://c:/Users/Momin/Downloads/ .... yourFile.ext"反而。您的路径无效,因为 \是转义字符

  • 不要忘记文件扩展名。
  • 同时输入 // (双斜杠)在驱动器前面:file://c
  • 不会有 space在你的路上。 /第 1A 课 - 建筑布局视频/ <-- incorrect

关于java - 在 JavaFX 中创建媒体播放器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31258086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com