java怎么播放mp4,如何在Java Swing应用程序中播放MP4视频

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.

解决方案

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.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值