[英]Event listener getting in way of changing slider value JavaFX
我正在使用 JavaFX 的 MediaPlayer 類制作一個隨着歌曲播放而移動的滑塊。 這完全正常,滑塊隨歌曲移動。 如果拖動滑塊,它會更改歌曲的位置(使用 .seek() 方法)。 單擊滑塊時會出現唯一的問題。 歌曲沒有移動,我認為這是因為聽者看着歌曲的位置仍在移動,這將滑塊移動到下一個位置。 我認為這阻止了用戶的點擊,但我不確定如何解決它。 這是否意味着暫停聽眾或我不確定的事情?
protected void updateValues() {
if (playTime != null && progressBar != null && volume != null) {
Platform.runLater(new Runnable() {
public void run() {
Duration currentTime = player.getCurrentTime();
duration = player.getMedia().getDuration();
playTime.setText(formatTime(currentTime, duration));
progressBar.setDisable(duration.isUnknown());
if (!progressBar.isDisabled()
&& duration.greaterThan(Duration.ZERO)
&& !progressBar.isValueChanging()) {
progressBar.setValue(currentTime.divide(duration).toMillis()
* 100.0);
}
}
});
}
}
progressBar.setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
player.seek(duration.multiply(progressBar.getValue()/100.0));
}
});
如果有幫助,我已經按照這個: https : //docs.oracle.com/javafx/2/media/playercontrol.htm
我懷疑您需要添加setOnMouseClicked以在鼠標單擊時觸發事件,
progressBar.setOnMouseClicked(e ->
player.seek(duration.multiply(progressBar.getValue()/100.0))
);
如果您想使用鼠標事件來處理這兩種情況,您可能需要添加兩個鼠標事件處理程序setOnMouseReleased
(用於拖動和釋放滑塊)和setOnMouseClicked
(用於單擊滑塊)。 但我建議您向滑塊添加ChangeListener ,而不是設置鼠標事件偵聽器。
progressBar.valueProperty().addListener((observable, oldValue, newValue) ->
player.seek(duration.multiply(newValue.doubleValue()/100.0))
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.