Winged
Winged

Reputation: 291

JavaFX and "MediaException: UNKNOWN ... Could not create player!"

Ok, so I need to program a more or less simple video player and I chosen JavaFX for this task. Till now I've written a VERY simple (and ought to be working) app, but it constantly throws the same error:

Exception in Application start method
Exception in thread "main" 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$1/1573468.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: MediaException: UNKNOWN : com.sun.media.jfxmedia.MediaException: Could not create player! : com.sun.media.jfxmedia.MediaException: Could not create player!
    at javafx.scene.media.MediaException.exceptionToMediaException(MediaException.java:146)
    at javafx.scene.media.MediaPlayer.init(MediaPlayer.java:511)
    at javafx.scene.media.MediaPlayer.<init>(MediaPlayer.java:414)
    at player.MoviePlayer.start(MoviePlayer.java:23)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
    at com.sun.javafx.application.LauncherImpl$$Lambda$55/17730007.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
    at com.sun.javafx.application.PlatformImpl$$Lambda$50/11354414.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
    at com.sun.javafx.application.PlatformImpl$$Lambda$53/4845991.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$51/26970262.run(Unknown Source)
    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$45(GtkApplication.java:126)
    at com.sun.glass.ui.gtk.GtkApplication$$Lambda$43/4959060.run(Unknown Source)
    ... 1 more
Caused by: com.sun.media.jfxmedia.MediaException: Could not create player!
    at com.sun.media.jfxmediaimpl.NativeMediaManager.getPlayer(NativeMediaManager.java:222)
    at com.sun.media.jfxmedia.MediaManager.getPlayer(MediaManager.java:104)
    at javafx.scene.media.MediaPlayer.init(MediaPlayer.java:467)
    ... 16 more

Process finished with exit code 1

I'm just beginning my adventure with JavaFX and before posting here I've read similar topics about this problem and most of the times the problem was with the type of media encoding. So first I've tried to play an output from Handbrake (Two-Pass .mp4) but it didn't worked. Then I downloaded a sample .flv file from HERE. It didn't worked too, so I tried a mp3 file, but the problem still persisted. I have even tried to compile it on Windows and on Linux, but nothing worked.

For the sake of clarity, the code looks like this:

package player;

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.scene.paint.Color;
import javafx.stage.Stage;

public class MoviePlayer extends Application {

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

    @Override
    public void start(Stage stage) throws Exception {
        Group root = new Group();

        Media media = new Media("file:///home/winged/IdeaProjects/MoviePlayer/video/barsandtone.flv");
        MediaPlayer player = new MediaPlayer(media);
        MediaView view = new MediaView(player);

        player.setAutoPlay(true);

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

        player.play();
    }
}

Upvotes: 4

Views: 12741

Answers (2)

Mateus Alves de Oliveira
Mateus Alves de Oliveira

Reputation: 1031

Make sure to install a media play software before. I had the same issue with JDK 20 On Ubuntu. I fixed that by installing a media player software like this:

sudo apt install ffmpeg

Then it will works:

Media media = new Media("file:///home/winged/IdeaProjects/MoviePlayer/video/barsandtone.flv");

Upvotes: 0

Winged
Winged

Reputation: 291

Ok, finally I found the cause of the problem - the way I passed the video path file was wrong:

Instead of using Media media = new Media("file:///home/winged/IdeaProjects/MoviePlayer/video/barsandtone.flv");

I ought use Media media = new Media(new File("file:///home/winged/IdeaProjects/MoviePlayer/video/barsandtone.flv").toURI().toString());

Upvotes: 5

Related Questions