JavaFx制作简易的mp4播放器

JavaFx制作简易的mp4播放器

在制作视频播放器时,用JAVA语言编写是个很方便的选择。虽然JavaFX从JDK11开始就分离,但是在网上稍微搜索下就可以找到解决方法(在命令行下运行的命令是最清楚的)。原谅我不太聪明,花了一整天,拼接和整合了一些代码,制作了下面的简单mp4播放器。功能很简单,界面功能:右边是扫描指定目录下的mp4文件,点击即可播放;下边是播放/暂停、从头开始播放、可拖动的进度条。专业个人使用还行,对在普适性上存在一些问题:1,播放MP4的文件名有要求,名字中不能空格、很多正常的符号等;2,点击右边按钮后存在播放不了视频的意外情况;3,进度条不能随着视频播放实时更新;4,媒体路径被写死。

1, windows系统下设置环境变量PATH_TO_FX=e:javafx-sdk-14.0.1lib

2, 编写Java文件:MediaDemo.java

  1 import javafx.application.Application;
  2 import javafx.stage.Stage;
  3 import javafx.geometry.Pos;
  4 import javafx.geometry.Orientation;
  5 import javafx.scene.Scene;
  6 import javafx.scene.control.Button;
  7 import javafx.scene.control.Label;
  8 import javafx.scene.control.Slider;
  9 import javafx.scene.control.RadioButton;
 10 import javafx.scene.control.ToggleGroup;
 11 import javafx.scene.layout.FlowPane;
 12 import javafx.scene.layout.BorderPane;
 13 import javafx.scene.layout.HBox;
 14 import javafx.scene.layout.VBox;
 15 import javafx.scene.layout.Region;
 16 import javafx.scene.layout.Priority;
 17 import javafx.scene.media.Media;
 18 import javafx.scene.media.MediaPlayer;
 19 import javafx.scene.media.MediaView;
 20 import javafx.util.Duration;
 21 import java.io.File;
 22 
 23 public class MediaDemo extends Application {
 24     private static final String MEDIA_DIR = "E:/xyz/";
 25     
 26     @Override
 27     public void start(Stage primaryStage) {
 28         MediaView mediaView = new MediaView();
 29         //mediaView.setSmooth​(true);
 30         
 31         Button playButton = new Button(">");
 32         playButton.setOnAction(e -> {
 33             MediaPlayer mPlayer = mediaView.getMediaPlayer();
 34             if (playButton.getText().equals(">")) {
 35                 mPlayer.play();
 36                 playButton.setText("||");
 37             } else {
 38                 mPlayer.pause();
 39                 playButton.setText(">");
 40             }
 41         });
 42     
 43     
 44         Button rewindButton = new Button("<<");
 45         rewindButton.setOnAction(e -> mediaView.getMediaPlayer().seek(Duration.ZERO));
 46         
 47         /*Slider slVolume = new Slider();
 48         slVolume.setPrefWidth(150);
 49         slVolume.setMaxWidth(Region.USE_PREF_SIZE);
 50         slVolume.setMinWidth(30);
 51         slVolume.setValue(50);
 52         mediaPlayer.volumeProperty().bind(
 53             slVolume.valueProperty().divide(100));
 54         */    
 55         // 播放进度条
 56         Slider slTime = new Slider();
 57         HBox.setHgrow(slTime,Priority.ALWAYS);
 58         slTime.setMinWidth(50);
 59         slTime.setMaxWidth(Double.MAX_VALUE);
 60         //slTime.setShowTickLabels(true);
 61         slTime.valueProperty().addListener(ov -> {
 62             Duration total = mediaView.getMediaPlayer().getTotalDuration();
 63             // 当前进度 total * progress / 100.0
 64             Duration s1 = total.multiply(slTime.getValue()).divide(100.0);
 65             mediaView.getMediaPlayer().seek(s1);
 66         });
 67         
 68         HBox progressHBox = new HBox(20);
 69         progressHBox.setAlignment(Pos.CENTER);
 70         progressHBox.setMinHeight(30);
 71         progressHBox.getChildren().add(slTime);
 72         
 73         HBox hBox = new HBox(10);
 74         hBox.setAlignment(Pos.CENTER);
 75         hBox.getChildren().addAll(playButton, rewindButton,
 76             new Label("Progress"), slTime);
 77         
 78         BorderPane pane = new BorderPane();
 79         pane.setCenter(mediaView);
 80         pane.setBottom(hBox);
 81         
 82         mp4List(pane, mediaView);
 83         
 84         Scene scene = new Scene(pane, 670, 500);
 85         primaryStage.setTitle("MeidaDemo");
 86         primaryStage.setScene(scene);
 87         primaryStage.show();
 88     }
 89     
 90     /**
 91     * 媒体播放列表
 92     */
 93     public void mp4List(BorderPane pane, MediaView mediaView) {
 94         File dir = new File(MEDIA_DIR);
 95         String[] mp4s = dir.list((f, n) -> n.matches(".*56mp4"));
 96         VBox vBox = new VBox();
 97         ToggleGroup group = new ToggleGroup();
 98         for(int i = 0; i < mp4s.length; i++) {
 99             RadioButton radioButt = new RadioButton(mp4s[i]);
100             radioButt.setToggleGroup(group);
101             radioButt.setOnAction(e -> {
102                 MediaPlayer mp = mediaView.getMediaPlayer();
103                 if (mp != null) {
104                     mp.dispose();
105                 }
106                 MediaPlayer mPlayer = new MediaPlayer(new Media("file:/" + MEDIA_DIR + radioButt.getText()));
107                 mPlayer.play();
108                 mediaView.setMediaPlayer(mPlayer);
109                 System.gc();
110             });
111             vBox.getChildren().add(radioButt);
112         }
113         pane.setRight(vBox);
114     }
115 }
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » JavaFx制作简易的mp4播放器