Insanovation
Insanovation

Reputation: 345

MediaPlayer in JavaFX

I am concerned with the fact that JavaFX has suffered some modifications since the day of this tutorial: http://www.youtube.com/watch?v=bWl98dhvf8Q .

Please, give me some hints for what should I change in this code, or is this kind of code valid, at all? Thank you.

Here's the code:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.Group;


public class BiPlayer extends Application {

    public static void main(String[] args){

        launch(args);

    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Group videoPane = new Group();

        Media media = new Media("C:\\Users\\Insanovation\\Downloads\\P.mp4");
        MediaPlayer player = new MediaPlayer(media);
        MediaView view = new MediaView(player);

        videoPane.getChildren().add(view);
        Scene scene = new Scene (videoPane, 400, 400, Color.BLACK);
        primaryStage.setScene(scene);
        primaryStage.show();

        player.play();
    }
}

Here's the output:

    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:483)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:363)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303)
    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:483)
    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:875)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
    at com.sun.javafx.application.LauncherImpl$$Lambda$48/1394438858.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: java.net.URISyntaxException: Illegal character in opaque part at index 2: C:\Users\Insanovation\Downloads\F.mp3
    at javafx.scene.media.Media.<init>(Media.java:383)
    at biplayer.BiPLAYER.start(BiPLAYER.java:24)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
    at com.sun.javafx.application.LauncherImpl$$Lambda$51/270894642.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
    at com.sun.javafx.application.PlatformImpl$$Lambda$44/1147985808.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/1822121612.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
    at com.sun.javafx.application.PlatformImpl$$Lambda$45/1267032364.run(Unknown Source)
    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$null$141(WinApplication.java:102)
    at com.sun.glass.ui.win.WinApplication$$Lambda$37/636718812.run(Unknown Source)
    ... 1 more
Caused by: java.net.URISyntaxException: Illegal character in opaque part at index 2: C:\Users\Insanovation\Downloads\F.mp3
    at java.net.URI$Parser.fail(URI.java:2848)
    at java.net.URI$Parser.checkChars(URI.java:3021)
    at java.net.URI$Parser.parse(URI.java:3058)
    at java.net.URI.<init>(URI.java:588)
    at javafx.scene.media.Media.<init>(Media.java:381)
    ... 15 more
Exception running application biplayer.BiPLAYER
Java Result: 1

Upvotes: 0

Views: 1704

Answers (1)

pulse0ne
pulse0ne

Reputation: 1102

Looking at the exception that you've copied and pasted, it's clear that the exception thrown is a URISyntaxException. It then lists the problem character, which is at index 2 (the backslash character).

If you look into the documentation of URIs here and here, you'll see that only forward slashes are acceptable in URIs. If you changed the backslashes in your path to forward slashes like so:

file:C:/Users/Insanovation/Downloads/F.mp4

I believe it should work.

Upvotes: 1

Related Questions