用java写一个视频播放器的代码
时间: 2024-01-05 13:03:41 浏览: 71
这是一个基于JavaFX的简单视频播放器的代码:
```java
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.layout.BorderPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.io.File;
public class VideoPlayer extends Application {
private final String MEDIA_URL = "file:///C:/video.mp4"; // 视频文件的路径
private final DoubleProperty rateProperty = new SimpleDoubleProperty(1.0); // 播放速度
private final DoubleProperty volumeProperty = new SimpleDoubleProperty(0.5); // 音量
@Override
public void start(Stage primaryStage) throws Exception {
// 加载视频文件
Media media = new Media(MEDIA_URL);
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
// 创建MediaView用于显示视频
MediaView mediaView = new MediaView(mediaPlayer);
// 创建Slider用于控制播放进度
Slider timeSlider = new Slider();
timeSlider.setMin(0);
mediaPlayer.setOnReady(() -> {
timeSlider.setMax(mediaPlayer.getTotalDuration().toSeconds());
});
mediaPlayer.currentTimeProperty().addListener((observable, oldValue, newValue) -> {
timeSlider.setValue(newValue.toSeconds());
});
timeSlider.setOnMouseClicked(event -> {
mediaPlayer.seek(Duration.seconds(timeSlider.getValue()));
});
// 创建Slider用于控制音量
Slider volumeSlider = new Slider();
volumeSlider.setMin(0);
volumeSlider.setMax(1);
mediaPlayer.volumeProperty().bindBidirectional(volumeProperty);
volumeSlider.valueProperty().bindBidirectional(volumeProperty);
// 创建Button用于播放/暂停视频
Button playButton = new Button("播放");
playButton.setOnAction(event -> {
if (mediaPlayer.getStatus() == MediaPlayer.Status.PLAYING) {
mediaPlayer.pause();
playButton.setText("播放");
} else {
mediaPlayer.play();
playButton.setText("暂停");
}
});
// 创建Button用于停止视频
Button stopButton = new Button("停止");
stopButton.setOnAction(event -> {
mediaPlayer.stop();
playButton.setText("播放");
});
// 创建Slider用于控制播放速度
Slider rateSlider = new Slider(0.5, 1.5, 1.0);
mediaPlayer.rateProperty().bindBidirectional(rateProperty);
rateSlider.valueProperty().bindBidirectional(rateProperty);
// 创建布局
BorderPane pane = new BorderPane();
pane.setCenter(mediaView);
pane.setBottom(timeSlider);
BorderPane.setMargin(timeSlider, new javafx.geometry.Insets(10, 0, 0, 0));
pane.setLeft(volumeSlider);
BorderPane.setMargin(volumeSlider, new javafx.geometry.Insets(0, 0, 0, 10));
pane.setTop(playButton);
BorderPane.setAlignment(playButton, javafx.geometry.Pos.CENTER);
pane.setRight(rateSlider);
BorderPane.setMargin(rateSlider, new javafx.geometry.Insets(0, 10, 0, 0));
pane.setBottom(stopButton);
BorderPane.setAlignment(stopButton, javafx.geometry.Pos.CENTER_RIGHT);
BorderPane.setMargin(stopButton, new javafx.geometry.Insets(0, 0, 10, 10));
// 创建场景
Scene scene = new Scene(pane, 640, 480);
// 设置舞台
primaryStage.setScene(scene);
primaryStage.setTitle("视频播放器");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
```
注意:这个代码需要将`MEDIA_URL`替换为你自己的视频文件路径。另外,需要在项目中添加JavaFX库的依赖,具体方法可以参考官方文档。
阅读全文