首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >javafx视频播放器timeSlider

javafx视频播放器timeSlider
EN

Stack Overflow用户
提问于 2016-06-11 23:36:00
回答 1查看 2.7K关注 0票数 2

如何使滑块在视频播放时移动?

初始化:

代码语言:javascript
运行
复制
BorderPane border = new BorderPane();
HBox bar = new HBox(10);
//Button play = new Button("Play");
//Button pause = new Button("Pause");

媒体:

代码语言:javascript
运行
复制
    Media media = new Media(Paths.get("C://video.mp4").toUri().toString());
    MediaPlayer player = new MediaPlayer(media);
    MediaView mediaView = new MediaView(player);

定位:

代码语言:javascript
运行
复制
    bar.getChildren().addAll(play,pause,timeSlider);
    bar.setAlignment(Pos.CENTER);
    border.setCenter(mediaView);
    border.setBottom(bar);

有什么建议可以让我的滑块在视频播放时移动吗?

编辑:

找到答案:

代码语言:javascript
运行
复制
player.currentTimeProperty().addListener((obs, oldTime, newTime) -> {
        if (!timeSlider.isValueChanging()) {
            timeSlider.setValue(newTime.toSeconds());
        }
    });
EN

回答 1

Stack Overflow用户

发布于 2017-11-20 13:14:49

我现在正在解决一个类似的问题。这对你来说应该是可行的:

代码语言:javascript
运行
复制
// Listen to the slider. When it changes, seek with the player.
// MediaPlayer.seek does nothing when the player is stopped, so the play/pause button's handler
// always sets the start time to the slider's current value, and then resets it to 0 after starting the player.
InvalidationListener sliderChangeListener = o-> {
    Duration seekTo = Duration.seconds(timeSlider.getValue());
    player.seek(seekTo);
});
timeSlider.valueProperty().addListener(sliderChangeListener);

// Link the player's time to the slider
player.currentTimeProperty().addListener(l-> {
    // Temporarily remove the listener on the slider, so it doesn't respond to the change in playback time
    // I thought timeSlider.isValueChanging() would be useful for this, but it seems to get stuck at true
    // if the user slides the slider instead of just clicking a position on it.
    timeSlider.valueProperty().removeListener(sliderChangeListener);

    // Keep timeText's text up to date with the slider position.
    Duration currentTime = player.getCurrentTime();
    int value = (int) currentTime.toSeconds();
    timeSlider.setValue(value);    

    // Re-add the slider listener
    timeSlider.valueProperty().addListener(sliderChangeListener);
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37765499

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档