How To Play MP4 Video In Java Swing App

孤街浪徒 提交于 2019-12-12 12:09:00

问题


Does anyone know any way I can play an .mp4 video file in a JPanel? I have tried JMF with .avi file but found no success and now I'm baffled and frustrated at how such a simple task of playing a video file is becoming so tedious.

Anyone out there please shed some light on what path I could take and I would greatly appreciate it.

I've heard of VLCJ but the issue is I can't guarantee that every machine running this app will have VLC player installed. Is there a way I can bundle VLC player in the distribution folder?

Originally, the video we're using is on Vimeo but it turns out it's practically impossible to embed it due a lack of API support and I thought okay I will just play it locally and then even that is becoming so difficult now.


回答1:


Thanks to @VGR for bringing JavaFX to my attention, I just integrated a JFXPanel into a JPanel of where I wanted the video to be. It's working perfectly fine in my case since it's a simple screen with one video to play.

Here's the full code snippet below:

private void getVideo(){
    final JFXPanel VFXPanel = new JFXPanel();

    File video_source = new File("tutorial.mp4");
    Media m = new Media(video_source.toURI().toString());
    MediaPlayer player = new MediaPlayer(m);
    MediaView viewer = new MediaView(player);

    StackPane root = new StackPane();
    Scene scene = new Scene(root);

    // center video position
    javafx.geometry.Rectangle2D screen = Screen.getPrimary().getVisualBounds();
    viewer.setX((screen.getWidth() - videoPanel.getWidth()) / 2);
    viewer.setY((screen.getHeight() - videoPanel.getHeight()) / 2);

    // resize video based on screen size
    DoubleProperty width = viewer.fitWidthProperty();
    DoubleProperty height = viewer.fitHeightProperty();
    width.bind(Bindings.selectDouble(viewer.sceneProperty(), "width"));
    height.bind(Bindings.selectDouble(viewer.sceneProperty(), "height"));
    viewer.setPreserveRatio(true);

    // add video to stackpane
    root.getChildren().add(viewer);

    VFXPanel.setScene(scene);
    //player.play();
    videoPanel.setLayout(new BorderLayout());
    videoPanel.add(VFXPanel, BorderLayout.CENTER);
}

Once the getVideo() function is made, I called it in the constructor of the JFrame to trigger it on the applications launch.



来源:https://stackoverflow.com/questions/52038982/how-to-play-mp4-video-in-java-swing-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!