Java FX Exception (Media Player Application)

2020-05-04 06:11发布

问题:

I'm just starting to use Java FX and cannot for the life of me figure out why this exception keeps occurring.

I have 2 classes. One called Main and the other called Player. Here is what they each look like....

Main Class

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("/Applications/Nacho.mp4");
        Scene scene = new Scene (player, 720, 480, Color.BLACK);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

Player Class

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 nPane;

    public Player(String file){
        media = new Media(file);
        player = new MediaPlayer(media);
        view = new MediaView(player);
        nPane = new Pane();
        nPane.getChildren().add(view);
        setCenter(nPane);
        player.play();
    }
}

Here is what the compiler spits out when I run the program...

START

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:497)
    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:497)
    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$152(LauncherImpl.java:182)
    at com.sun.javafx.application.LauncherImpl$$Lambda$51/1323468230.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: uri.getScheme() == null! uri == 'Applications/Nacho.mp4'
    at com.sun.media.jfxmedia.locator.Locator.(Locator.java:211)
    at javafx.scene.media.Media.(Media.java:391)
    at application.Player.(Player.java:16)
    at application.Main.start(Main.java:13)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
    at com.sun.javafx.application.LauncherImpl$$Lambda$54/1046396900.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/186276003.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda$49/287662571.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/237061348.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application application.Main

FINISH

I know that the issue occurs as soon as the Player object is instantiated in Main. As soon the Player object uses it's constructor and creates a Media object, which gets passed the String file location, that's when I get the error. I ran the program with an empty constructor for the Player object and it ran fine so the issue is clearly in my Player Class, again when the Media object is instantiated. Am I passing the correct parameters? "/Applications/Nacho.mp4"

If someone can please tell me how to fix this, I would greatly appreciate it. I literally can't move forward with this project otherwise. Thank you!

回答1:

Nevermind, I figured it out, the problem was passing the parameter to the Media object. I'm used to C++ syntax and did not realize that the proper way to do it is

media = new Media("file:///Applications/Nacho.mp4");


回答2:

This error usually occurs due to wrongly input of the file address. The correct way to write the address is file:///C:/abc.mp4.

More clearly:

Object b = new Object("file:///C:/abc.mp4");