How do I make JavaFX MediaView stretch media to fill parent container?

前端 未结 4 1034
故里飘歌
故里飘歌 2020-12-17 20:38

I\'m trying to make my video\'s dimensions stretch automatically and fill the MediaView and maintain the original aspect ratio of the video. Basically, I want my MediaPlayer

4条回答
  •  一向
    一向 (楼主)
    2020-12-17 21:32

    Here's an example of stretching to the containing Scene and the scene is set by the Dimension "size":

    final MediaPlayer videoPlayer = new MediaPlayer(
        new Media(new File(videoPath).toURI().toString()));
    MediaView mv = new MediaView(videoPlayer);
    DoubleProperty mvw = mv.fitWidthProperty();
    DoubleProperty mvh = mv.fitHeightProperty();
    mvw.bind(Bindings.selectDouble(mv.sceneProperty(), "width"));
    mvh.bind(Bindings.selectDouble(mv.sceneProperty(), "height"));
    mv.setPreserveRatio(true);
    
    setScene(new Scene(new Group(mv), size.getWidth(), size.getHeight()));
    

提交回复
热议问题