【问题标题】:JavaFx Video Dimension OUT of screenJavaFx 视频尺寸超出屏幕
【发布时间】:2012-07-05 03:24:40
【问题描述】:

我最近发现 javafx 2.1 对我制作视频播放器的项目非常有用,但经过 成功我遇到了视频大小尺寸的问题。换句话说,当我运行 程序和视频正常播放 我看不到整个视频,因为它的尺寸 比我的屏幕分辨率大。我可以在下面的代码中做什么来调整 windows7 64bit 中视频的实际大小:

public class HelloFx extends Application {

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

    @Override
    public void start(final Stage stage) throws Exception {
         stage.setTitle("Movie Player");
         final BorderPane root = new BorderPane();

         final Media media = new Media("file:///Users//user//Videos//Sintel.mp4");
         final MediaPlayer player = new MediaPlayer(media);
         final MediaView view = new MediaView(player);

         // System.out.println("media.width: "+media.getWidth());
         root.getChildren().add(view);

         final Scene scene = new Scene(root, 400, 400, Color.BLACK);


         stage.setScene(scene);
         stage.show();
         player.play();
         player.setOnReady(new Runnable() {
             @Override
             public void run() {
                 int w = player.getMedia().getWidth();
                 int h = player.getMedia().getHeight();

                 stage.setMinWidth(w);
                 stage.setMinHeight(h);


             }
         });
          //player.play();

    }
}

【问题讨论】:

    标签: java windows javafx-2


    【解决方案1】:

    JavaFX 2 MediaView 类有 2 个可以提供帮助的函数。它们是 .setFitHeight() 和 .setFitWidth() 。

    因此,您可以让您的舞台设置屏幕的大小,而不是让媒体决定屏幕的大小...

     public void run() {
                     int w = stage.getWidth(); // player.getMedia().getWidth();
                     int h = stage.getHeight(); // player.getMedia().getHeight();
    
                     // stage.setMinWidth(w);
                     // stage.setMinHeight(h);
                     // make the video conform to the size of the stage now...
                     player.setFitWidth(w);
                     player.setFitHeight(h);
    
    
                 }
    

    那么视频应该适合舞台。上面的代码非常粗糙,您可能希望更好地“缩放”视频,即:找到媒体宽度与舞台宽度和媒体高度与舞台高度的比率......但上面的代码应该让你开始。

    【讨论】:

    • 非常感谢您的帮助。我会为您的帮助 +1,但我没有足够的声誉。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多