使用 vlcj-javafx-demo 开发一个播放器,但它看起来有些组件 UI 在设置全屏时没有正确更新

use vlcj-javafx-demo develop a player, but it looks some components UI not update correctly when set full screen

我尝试使用vlcj-javafx-demo开发一个视频播放器,我把进度条(Slider)放在视频层的StackPane上。一开始,它看起来运行良好,但是当我将应用程序设置为最大或全屏时,它看起来某些组件 UI 没有正确更新。我该如何更正它?

非常感谢!

通常: [1]: https://i.stack.imgur.com/bbE51.png

通常: [2]: https://i.stack.imgur.com/Plsb1.png 红色是背景色。

代码:

package my.javafx.myplayer;

import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.DoubleProperty;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.Slider;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;
import uk.co.caprica.vlcj.factory.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.base.MediaPlayer;
import uk.co.caprica.vlcj.player.base.MediaPlayerEventAdapter;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;

import java.util.ArrayList;
import java.util.List;

import static uk.co.caprica.vlcj.javafx.videosurface.ImageViewVideoSurfaceFactory.videoSurfaceForImageView;

/**
 *
 */
public class VlcjJavaFxApplication extends Application {

    private final MediaPlayerFactory mediaPlayerFactory;

    private final EmbeddedMediaPlayer embeddedMediaPlayer;

    private ImageView videoImageView;
    
    Slider progress=new Slider();

    public VlcjJavaFxApplication() {
        this.mediaPlayerFactory = new MediaPlayerFactory();
        this.embeddedMediaPlayer = mediaPlayerFactory.mediaPlayers().newEmbeddedMediaPlayer();
        this.embeddedMediaPlayer.events().addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
            @Override
            public void mediaPlayerReady(uk.co.caprica.vlcj.player.base.MediaPlayer mediaPlayer) {
                Platform.runLater(()->{
                    progress.setValue(0);
                    progress.setMax(embeddedMediaPlayer.media().info().duration());
                    
                    progress.setMin(0);
                });
            }
            @Override
            public void playing(uk.co.caprica.vlcj.player.base.MediaPlayer mediaPlayer) {
                
            }

            @Override
            public void paused(uk.co.caprica.vlcj.player.base.MediaPlayer mediaPlayer) {
            }

            @Override
            public void stopped(uk.co.caprica.vlcj.player.base.MediaPlayer mediaPlayer) {
            }

            @Override
            public void timeChanged(uk.co.caprica.vlcj.player.base.MediaPlayer mediaPlayer, long newTime) {
                Platform.runLater(()->{
                    progress.setValue(newTime);
                });
                
            }
        });
    }

    @Override
    public void init() {
        this.videoImageView = new ImageView();
        this.videoImageView.setPreserveRatio(true);

        embeddedMediaPlayer.videoSurface().set(videoSurfaceForImageView(this.videoImageView));
    }

    @Override
    public final void start(Stage primaryStage) throws Exception {
        List<String> params = new ArrayList<String>();
        params.add("/Users/baixq/Downloads/妙味课堂xhtml+css2/妙味课堂-XHTMLCSS2整站视频教程-4.avi");
        if (params.size() != 1) {
            System.out.println("Specify a single MRL");
            System.exit(-1);
        }
        StackPane root=new StackPane();
        //BorderPane root = new BorderPane();
        root.setStyle("-fx-background-color: black;");

        videoImageView.fitWidthProperty().bind(root.widthProperty());
        videoImageView.fitHeightProperty().bind(root.heightProperty());

        root.widthProperty().addListener((observableValue, oldValue, newValue) -> {
            // If you need to know about resizes
        });

        root.heightProperty().addListener((observableValue, oldValue, newValue) -> {
            // If you need to know about resizes
        });
        
        

        Scene scene = new Scene(root, 1200, 675, Color.RED);
        primaryStage.setTitle("vlcj JavaFX");
        primaryStage.setScene(scene);
        
        AnchorPane contrlBox=new AnchorPane();//操作面板上的控制模块
        contrlBox.prefWidthProperty().bind(root.widthProperty());
        contrlBox.prefHeightProperty().bind(root.heightProperty().multiply(0.1));
        
        BorderPane controlBar=new BorderPane();
        controlBar.setStyle("-fx-background-color: #130c0e;");
        controlBar.prefWidthProperty().bind(root.widthProperty());
        controlBar.prefHeightProperty().bind(root.heightProperty().multiply(0.1));
        controlBar.setCenter(progress);
        Button fullScreen=new Button("全屏");
        controlBar.setRight(fullScreen);
        
        contrlBox.getChildren().add(controlBar);
        contrlBox.setBottomAnchor(controlBar, 0.0);
        
        
        root.getChildren().addAll(videoImageView,contrlBox);
        
        
        primaryStage.show();
        embeddedMediaPlayer.media().play(params.get(0));
        
        fullScreen.setOnAction(event->{
            primaryStage.setFullScreen(true);
        });

        root.setOnMouseEntered(event->{
            Platform.runLater(()->{
                FadeTransition ft = new FadeTransition(Duration.millis(500), contrlBox);
                ft.setFromValue(0.0);
                ft.setToValue(1);
                //ft.setCycleCount(Timeline.INDEFINITE);
                ft.setAutoReverse(false);
                
                
                ft.play();
                contrlBox.setVisible(true);
            });
            
            
            
        });

        root.setOnMouseExited(event->{
            Platform.runLater(()->{
                FadeTransition ft = new FadeTransition(Duration.millis(500), contrlBox);
                ft.setFromValue(1);
                ft.setToValue(0.0);
                ft.setAutoReverse(false);
                
                
                
                ft.play();
                contrlBox.setVisible(false);
            });
        });
        
        //embeddedMediaPlayer.controls().setPosition(0.4f);
    }

    @Override
    public final void stop() {
        embeddedMediaPlayer.controls().stop();
        embeddedMediaPlayer.release();
        mediaPlayerFactory.release();
    }

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

您似乎正在使用 Linux OS,请尝试在启动 JVM 时传递以下一个或多个系统属性:

-Dprism.dirtyopts=false
-Dprism.forceUploadingPainter=true

这在“Linux 注释”下提到:https://github.com/caprica/vlcj-javafx-demo/tree/vlcj-5.x

我之前在 Linux 上看到过类似的绘画故障,并且在所有情况下都使用这些属性,至少对我来说,只对性能造成很小的影响就可以解决问题 - 即使在执行类似 full-screen 九个并发媒体播放器的网格,其中每个都有一个动画视频控件覆盖。