如何在linux上的javaFX中播放视频

问题描述 投票:3回答:1

我正在尝试制作一个多平台的JAVAFX桌面应用程序,我希望它能够播放视频。这是一个示例代码 -

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import java.io.File;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        AnchorPane anchorPane = new AnchorPane();
        MediaView mediaview = new MediaView();
        anchorPane.getChildren().addAll(mediaview);
        primaryStage.setTitle("Linux Video Play Test");
        primaryStage.setScene(new Scene(anchorPane, 540, 210));
        primaryStage.show();

        File file=new File("video.flv");
        Media media=new Media(file.toURI().toString());
        MediaPlayer mediaplayer = new MediaPlayer(media);
        mediaview.setMediaPlayer(mediaplayer);
        mediaplayer.play();
    }


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

'video.flv'出现在同一个文件夹中,编解码器信息可以找到here

上面的代码在Windows上完美运行(Win 10,x64,JDK 1.8.92),但在Linux上出现以下错误(Ubuntu 16.10,x64,JDK 1.8.112) -

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:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    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 Main.start(Main.java:24)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$49(GtkApplication.java:139)
    ... 1 more
Caused by: com.sun.media.jfxmedia.MediaException: Could not create player!
    at com.sun.media.jfxmediaimpl.NativeMediaManager.getPlayer(NativeMediaManager.java:274)
    at com.sun.media.jfxmedia.MediaManager.getPlayer(MediaManager.java:118)
    at javafx.scene.media.MediaPlayer.init(MediaPlayer.java:467)
    ... 11 more

Process finished with exit code 1

如何让它在Linux上运行?我知道有一些依赖,如'glib','libavformat'等。但是当我尝试时

sudo apt-get install glib

它说

E: Unable to locate package glib

与libavformat53相同的问题。请注意,视频格式限制不是问题,我将向我的应用的用户提供视频文件。因此,如果我能够设法使一个格式工作(例如,flv),那就可以了,我可以将所有视频转换为该格式。此外,我不希望我的最终用户遇到任何麻烦使其工作,如在终端上键入命令等。我打算将应用程序分发为“.deb”文件,我希望它是这样的用户可以直接下载deb文件并单击并安装(通过ubuntu软件中心等)并且可以正常工作(在linux上播放视频)。安装完成后,启动应用程序将首先激活一个bash脚本,然后启动主jar。我正在考虑两种选择 -

1) - 在deb包本身中指定所有依赖项(glib,libavformat),以便随应用程序一起安装。但问题是我在ubuntu 16.10上,而旧的libs(glib2.28和libavformat53)在repo中不可用。我可以找到glib2.50和libavformat57,但它们似乎不起作用(或者我可能做错了什么)。那么有没有办法在deb文件中指定依赖项和repo,以便轻松安装?如果没有,我可能希望用户提供一次root访问权限,这样我就可以通过bash脚本安装这些库,这个脚本会在app启动时触发,这可能吗?

2) - 我也在考虑完全抛弃“javafx.scene.media.MediaPlayer”并使用像here这样的第三方库。但是这些解决方案看起来都非常复杂,所以如果有人能指出正确的方向帮助我。大多数这些库似乎都在copyleft(GPL3 / LGPL3)下,我想让我的应用程序关闭源代码。那可能吗?

Ps:这是我在Stackoverflow上的第一篇文章,所以如果我犯了任何错误,请原谅我。还有,对不起英语不好。 :)

java linux video javafx javafx-8
1个回答
1
投票

Mediaplayer需要库libavformat54及其所有需要的库。我用Debian 9(拉伸)试了一下并得到了它。所需的库主要在Debian 7(wheezy)中,还有一些仍在Debian 9中。您必须手动加载所有需要的库,这些库不属于您的发行版,并使用dpkg进行安装。使用libmp3lame0我遇到的问题最多,因为你需要纯库,但是你会找到一个重新打包的版本。 libavformat54需要libavcodec54和libavutil51_1.0.10。安装这两个库后,可以安装libavformat54。因此,从两个库开始,看看他们需要什么,并从互联网上获取所需的库。我认为最好的方面是pkgs.org来获取所需的库。

© www.soinside.com 2019 - 2024. All rights reserved.