【发布时间】:2014-07-04 12:23:41
【问题描述】:
我正在编写一个程序来从 url 流式传输视频并放置一个不确定的进度条来显示视频加载。加载视频是写在任务中的,当任务完成时进度条应该隐藏。视频加载没有任何错误,但我的问题是进度条永远不会出现!
这是我的SSCCE
package application;
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.AnchorPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Popup;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
AnchorPane root = new AnchorPane();
MediaView mediaView = new MediaView();
Task task = new Task<Media>() {
@Override
protected Media call()throws Exception {
System.out.println("Loading...");
Media media = new Media("http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4");
return media;
}
};
ProgressBar progressBar = new ProgressBar();
progressBar.setProgress(ProgressIndicator.INDETERMINATE_PROGRESS);
progressBar.progressProperty().bind(task.progressProperty());
Popup newPopup = new Popup();
newPopup.getContent().add(progressBar);
task.setOnSucceeded(new EventHandler<Event>() {
@Override
public void handle(Event event) {
newPopup.hide();
MediaPlayer mediaplayer = new MediaPlayer((Media)task.getValue());
mediaView.setMediaPlayer(mediaplayer);
mediaplayer.setAutoPlay(true);
}
});
root.getChildren().add(mediaView);
Scene scene = new Scene(root,640,480);
primaryStage.setScene(scene);
primaryStage.show();
newPopup.show(primaryStage);
new Thread(task).start();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
【问题讨论】:
标签: progress-bar task javafx-8