JavaFX实现视频播放功能
一、前言
最近使用javaFx写了个简单的视频播放功能,可以实现打开本地视频播放。
二、实现
1.使用jdk8自带的javaFx包实现,首先定义一个类VideoPlayer。
代码如下:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.InvalidationListener;
import javafx.beans.property.Property;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaException;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.FileChooser;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.util.Duration;import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.util.concurrent.atomic.AtomicBoolean;public class VideoPlayer extends Application {Button play,full,openFile;MediaPlayer mediaPlayer;MediaView mediaView;public static void main(String[] args) {launch(args);}@Overridepublic void start(Stage primaryStage) throws MalformedURLException, URISyntaxException {openFile=new Button();openFile.setText("打开文件");openFile.setTranslateX(-300.0);openFile.setTranslateY(340.0);// 将MediaView加入到布局容器StackPane root = new StackPane();// 创建场景Scene scene = new Scene(root, 1000, 800);root.getChildren().add(openFile);primaryStage.setScene(scene);primaryStage.show();openFile.setOnMouseClicked(o->{mediaView = new MediaView(mediaPlayer);openFile(primaryStage);// 加载播放图像Image image = new Image(new File("image/play.png").toURI().toString());ImageView imageView = new ImageView(image);imageView.setFitHeight(45); // 设置图标大小imageView.setFitWidth(45);imageView.setPreserveRatio(true);full=new Button();play=new Button();play.setBackground(null);play.setTranslateX(0.0);play.setTranslateY(340.0);play.setGraphic(imageView);play.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);// 创建MediaPlayer对象mediaPlayer.setAutoPlay(false); // 设置自动播放// 创建MediaView对象mediaView.setFitWidth(990);mediaView.setFitHeight(600);mediaView.setSmooth(true);// 将MediaView加入到布局容器root.getChildren().add(mediaView);root.getChildren().add(play);root.getChildren().add(full);// 创建场景scene.setOnMouseEntered(m->{if (!primaryStage.isFullScreen()) {play.setVisible(true);full.setVisible(true);}});scene.setOnMouseExited(m->{if (!primaryStage.isFullScreen()) {play.setVisible(false);full.setVisible(false);}});// 创建进度条ProgressBar progressBar = new ProgressBar(0);progressBar.setStyle("-fx-height: 8;");progressBar.setStyle("-fx-padding: -1;");Slider timeSlider = new Slider(0,0,0);// 当媒体元数据加载完成后,获取总播放时长mediaPlayer.setOnReady(() -> {Duration totalDuration = mediaPlayer.getTotalDuration();timeSlider.setMax(totalDuration.toSeconds());progressBar.progressProperty().bind(timeSlider.valueProperty().divide(totalDuration.toSeconds()));System.out.println("总播放时长: " + totalDuration.toSeconds() + " 秒");});mediaPlayer.setOnPlaying(() -> {Platform.runLater(()->{if (timeSlider.isFocused()) {return;}timeSlider.setValue(mediaPlayer.getCurrentTime().toSeconds());});});timeSlider.setOnMouseReleased(e -> {// 当拖动结束时更新进度条timeSlider.setValue(mediaPlayer.getCurrentTime().toSeconds());if (timeSlider.getValue()>=timeSlider.getMax()) {play.setGraphic(imageView);}});.........
2.实现的打开文件功能。
private void openFile(Stage primaryStage) {FileChooser fileChooser = new FileChooser();fileChooser.setTitle("打开文件");fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("文件", "*.mp4","*.mp3","*.wav"));File file = fileChooser.showOpenDialog(null);if (file != null) {try {URI uri = Paths.get(file.getAbsolutePath()).toUri();Media media = new Media(uri.toString());if (mediaPlayer != null) {mediaPlayer.dispose();}mediaPlayer = new MediaPlayer(media);mediaView = new MediaView(mediaPlayer);String mediaTitle = "player - " + file.getName();//((Stage) mediaView.getScene().getWindow()).setTitle(mediaTitle);primaryStage.setTitle(mediaTitle);} catch (MediaException e) {Alert alert = new Alert(Alert.AlertType.ERROR);alert.setTitle("错误");alert.setHeaderText("无法打开文件");alert.setContentText("打开文件时发生错误。");alert.showAndWait();}}}
3.实现后的效果如下:
完整代码如下:
javaFX实现视频播放源码