gpt4 book ai didi

Javafx获取作为媒体加载的视频的宽度和高度(分辨率)

转载 作者:行者123 更新时间:2023-12-01 09:18:26 32 4
gpt4 key购买 nike

我正在使用 JavaFX 开发视频播放器。我正在将视频从本地文件系统加载到 JavaFX 类媒体中。然后我想获取宽度和高度,以便我可以将场景的尺寸设置为该大小:

Media media = new Media(
"file:/D:/Videos/Cosmos/Cosmos.A.Space.Time.Odyssey.S01E02.HDTV.x264.PROPER-LOL.mp4"
);
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
MediaView mediaView = new MediaView(mediaPlayer);
root.getChildren().add( mediaView);
Scene scene = new Scene(root, media.getWidth(), media.getHeight());

但是,当我运行此代码时,media.getWidth() 和 media.getHeight() 都返回 0。如何获取尺寸?

最佳答案

来自Media class documentation :

The media information is obtained asynchronously and so not necessarily available immediately after instantiation of the class. All information should however be available if the instance has been associated with a MediaPlayer and that player has transitioned to MediaPlayer.Status.READY status.

当 MediaPlayer 处于 READY 状态时,该大小可用。最简单的方法是在播放媒体之前设置一个处理程序,该处理程序将在窗口上调用 sizeToScene():

mediaPlayer.setOnReady(() -> stage.sizeToScene());
mediaPlayer.setAutoPlay(true);

// ...

Scene scene = new Scene(root);

关于Javafx获取作为媒体加载的视频的宽度和高度(分辨率),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40346992/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com