题目:**16.24(修改编程练习题16-14)
增加一个滑动条让用户可以为视频设置当前时间,增加一个标签显示当前时间和视频的整体时间,如图16-47a所示。整个时间是5分钟03秒,当前时间是3分58秒。当播放视频时,滑条值和当前时间持续更新。
-
代码示例
编程练习题16_24SetVideoTime.java
package chapter_16;
import java.nio.file.Paths;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import javafx.util.Duration;
public class 编程练习题16_24SetVideoTime extends Application{
private int sencod;
@Override
public void start(Stage primaryStage) throws Exception {
//如果运行后没有出现视频请多run几次
String filePath = Paths.get("C:\\Users\\Lenovo\\eclipse-workspace\\JavaFX\\src\\Video\\钢琴曲《起风了》.mp4").toAbsolutePath().toString();
String MEDIA_URL =
"file:///" + filePath.replace("\\", "/");
Media media = new Media(MEDIA_URL);
MediaPlayer mediaPlayer = new MediaPlayer(media);
MediaView mediaView = new MediaView(mediaPlayer);
Button playButton = new Button(">");
playButton.setOnAction(e ->{
if(playButton.getText().equals(">")) {
mediaPlayer.play();
playButton.setText("||");
}else {
mediaPlayer.pause();
playButton.setText(">");
}
});
Slider slTime = new Slider(0,100,0);
Label lbTime = new Label(formatTime(media.getDuration()));
mediaPlayer.currentTimeProperty().addListener((obs, oldVal, newVal) -> {
lbTime.setText(formatTime(newVal) + "/" + formatTime(media.getDuration()));
if (newVal != null && media.getDuration().isUnknown() == false) {
sencod = (int) media.getDuration().toSeconds();
slTime.setMax(sencod);
slTime.setValue(newVal.toSeconds());
}
});
slTime.valueProperty().addListener((obs, oldValue, newValue) -> {
Duration currentTimeAsDuration = Duration.seconds(newValue.doubleValue());
lbTime.setText(formatTime(currentTimeAsDuration) + "/" + formatTime(media.getDuration()));
});
slTime.valueChangingProperty().addListener((obs, oldValue, newValue) -> {
mediaPlayer.pause();
playButton.setText(">");
mediaPlayer.seek(Duration.seconds(slTime.getValue()));
});
slTime.addEventHandler(MouseEvent.MOUSE_RELEASED, event -> {
if (mediaPlayer.getStatus() == MediaPlayer.Status.PAUSED || mediaPlayer.getStatus() == MediaPlayer.Status.STOPPED) {
mediaPlayer.play();
playButton.setText("||");
}
});
Slider slVolume = new Slider();
slVolume.setPrefWidth(150);
slVolume.setMaxWidth(Region.USE_PREF_SIZE);
slVolume.setMinWidth(30);
slVolume.setValue(50);
mediaPlayer.volumeProperty().bind(
slVolume.valueProperty().divide(100));
HBox hBox = new HBox(10);
hBox.setAlignment(Pos.CENTER);
hBox.getChildren().addAll(playButton,new Label("Time"),slTime,lbTime,new Label("Volume"),slVolume);
BorderPane pane = new BorderPane();
pane.setCenter(mediaView);
pane.setBottom(hBox);
Scene scene = new Scene(pane, 650, 500);
primaryStage.setTitle("编程练习题16_24SetVideoTime");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
private String formatTime(Duration duration) {
if (duration == null || duration.isUnknown()) {
return "--:--";
}
long seconds = (long) duration.toSeconds();
int minutes = (int) (seconds / 60);
int remainderSeconds = (int) (seconds % 60);
return String.format("%02d:%02d", minutes, remainderSeconds);
}
}
-
结果展示