JavaFXMediaPlayer抛出java.lang.reflect.InvocationTargetException

JavaFXMediaPlayer抛出java.lang.reflect.InvocationTargetException,java,javafx,uri,media-player,Java,Javafx,Uri,Media Player,我一直在尝试JavaFXMediaPlayer类,并在不同的线程中发现了以下示例代码: import java.net.URI; import java.net.URISyntaxException; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.media.Media; import javafx.scene.me

我一直在尝试JavaFXMediaPlayer类,并在不同的线程中发现了以下示例代码:

import java.net.URI;
import java.net.URISyntaxException;

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 test extends Application {

    @Override
    public void start(Stage primaryStage)
    {
        //Add a scene
        Group root = new Group();
        Scene scene = new Scene(root, 500, 200);
        URI file = null;
        try {
            file = new URI("/home/thomas/voodoo.mp3");
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("POOR URI SYNTAX");
        }
        Media pick = new Media(file.toString());
        MediaPlayer player = new MediaPlayer(pick);
        player.play();

        //Add a mediaView, to display the media. Its necessary !
        //This mediaView is added to a Pane
        MediaView mediaView = new MediaView(player);
        ((Group)scene.getRoot()).getChildren().add(mediaView);

        //show the stage
        primaryStage.setTitle("Media Player");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    public static void main(String[] args) {
         launch(args);
    }
}
我调整了文件路径以指向真实的音频文件(/home/thomas/voodoo.mp3),并尝试运行它,但控制台输出如下:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    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(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
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$launchApplication$1(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: uri.getScheme() == null! uri == '/home/thomas/voodoo.mp3'
    at com.sun.media.jfxmedia.locator.Locator.<init>(Locator.java:211)
    at javafx.scene.media.Media.<init>(Media.java:393)
    at test.start(test.java:28)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$5(GtkApplication.java:139)
    ... 1 more
Exception running application test

问题是您的url
/home/thomas/voodoo.mp3
无效,正如错误消息中所述,确实缺少该方案(
uri.getScheme()==null!uri='/home/thomas/voodoo.mp3'
),它应该以
file://
开头,因为它是本地文件系统中的一个文件,所以应该是
file:///home/thomas/voodoo.mp3

但是,由于必须确保
URL
编码正确(例如,空格应替换为
%20
),并且需要提供有效的方案,因此像您目前这样构建
URI
非常容易出错,因此您最好使用
新文件(path).toURI().toString()
按照建议。

试试

Media pick = new Media(new File("/home/thomas/voodoo.mp3").toURI().toString());

仍然有问题。请看上面。好的,那么您正在运行什么操作系统和jdk呢?你能解决你的问题吗?我在win10、JDK1.80 e u101i和Manjaro linux上运行这个程序没有问题。我已经安装了ffmpeg。还有其他我需要的软件包吗?我真的不知道。你的问题可能与你的工作有关吗。它被固定在8u102中。因此,如果您还没有升级jdk,我建议您升级您的jdk。现在您正在向媒体构造函数传递一个完整且正确的URI,该文件可能无法读取,或者不是有效的MP3文件。看见
Media pick = new Media(new File("/home/thomas/voodoo.mp3").toURI().toString());