JavaFX和" MediaException:UNKNOWN ...无法创建播放器!"

时间:2014-10-29 08:05:28

标签: java javafx

好的,所以我需要编写一个或多或少简单的视频播放器,我选择了JavaFX来完成这项任务。直到现在我已经写了一个非常简单(并且应该工作)的应用程序,但它不断抛出同样的错误:

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

我刚刚开始使用JavaFX进行冒险,在发布此处之前,我已经阅读了有关此问题的类似主题,并且大多数情况下问题出在媒体编码类型上。所以首先我尝试使用Handbrake(双通.mp4)输出,但它没有奏效。然后我从HERE下载了一个示例.flv文件。它也没有用,所以我尝试了一个mp3文件,但问题仍然存在。我甚至试图在Windows和Linux上编译它,但没有任何效果。

为了清楚起见,代码如下所示:

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();
    }
}

1 个答案:

答案 0 :(得分:3)

好的,最后我找到了问题的原因 - 我传递视频路径文件的方式是错误的:

而不是使用Media media = new Media("file:///home/winged/IdeaProjects/MoviePlayer/video/barsandtone.flv");

我应该使用Media media = new Media(new File("file:///home/winged/IdeaProjects/MoviePlayer/video/barsandtone.flv").toURI().toString());