如何在Java Swing应用程序中播放MP4视频 [英] How To Play MP4 Video In Java Swing App

查看:1232
本文介绍了如何在Java Swing应用程序中播放MP4视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道我可以在JPanel中播放.mp4视频文件的任何方法吗? 我曾经尝试过将JMF与.avi文件结合使用,但是没有成功,但现在,让如此简单的视频文件播放任务变得如此乏味,令我感到困惑和沮丧.

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.

我听说过VLCJ,但问题是我不能保证每台运行此应用程序的计算机都将安装VLC播放器.有什么方法可以将VLC播放器捆绑在分发文件夹中?

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?

最初,我们正在使用的视频是在Vimeo上发布的,但由于缺乏API支持,事实证明几乎无法嵌入该视频,我想可以在本地播放,现在变得如此困难.

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.

推荐答案

由于@VGR引起了我对JavaFX的关注,我只是将JFXPanel集成到了我希望视频放置的JPanel中.就我而言,它工作得很好,因为它是一个可以播放一个视频的简单屏幕.

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.

以下是完整的代码段:

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

一旦创建了getVideo()函数,我便在JFrame的构造函数中对其进行了调用,以在应用程序启动时触发它.

这篇关于如何在Java Swing应用程序中播放MP4视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆