package javaseniorprograme; import javafx.application.Application; import javafx.event.EventHandler; import javafx.geometry.Insets; 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.media.Media; import javafx.scene.media.MediaPlayer; import javafx.scene.media.MediaView; import javafx.stage.Stage; import javafx.util.Duration; /** * 16.24 * @author ASUS */ public class MediaDemo extends Application{ @Override public void start(Stage stage){ // 利用目标多媒体URL来构建一个Media实例 Media mt = new Media("file:/C:/Users/ASUS/Documents/NetBeansProjects/javaSeniorPrograme/Vex.mp4"); MediaPlayer mp = new MediaPlayer(mt); MediaView mv = new MediaView(mp); // 构建Label实例 Label text1 = new Label("Time"); Label text2 = new Label("Volume"); Label text3 = new Label("00:00:00/00:02:27"); Label text4 = new Label("0"); // 构建Slider滑块控件实例 // 用于表示视频播放进度的滑块 Slider sld_time = new Slider(); // 初始值为0 sld_time.setValue(0); // 最大值为147,与目标多媒体大小相关 // 147可理解为视频的总时间为147秒 sld_time.setMax(147); sld_time.setBlockIncrement(5); // 通过方法构建播放按钮 Button btn = playButton(mp,sld_time); /** * 对多媒体的currentTimeProperty添加一个监听器 * 来实现滑块与当前时间的持续更新 */ mp.currentTimeProperty().addListener(e->{ if(sld_time.getValue()==sld_time.getMax()){ btn.setText(">>"); sld_time.setValue(0); mp.seek(Duration.ZERO); mp.pause(); } sld_time.setValue(Math.floor(mp.getCurrentTime().toSeconds())); Integer a = new Integer((int)Math.floor(mp.getCurrentTime().toSeconds())/60); Integer b = new Integer((int)Math.floor(mp.getCurrentTime().toSeconds())%60/10); Integer c = new Integer((int)Math.floor(mp.getCurrentTime().toSeconds())%60%10); text3.setText("00:0"+a.toString()+":"+b.toString()+c.toString()+"/00:02:27"); }); // 用于表示视频播放音量的滑块 Slider sld_vol = new Slider(); sld_vol.setValue(0); // 属性绑定 mp.volumeProperty().bind(sld_vol.valueProperty().divide(10)); // HBox用来布局 HBox hbox = new HBox(); hbox.setAlignment(Pos.CENTER); hbox.setPadding(new Insets(10)); hbox.setSpacing(10); // 添加控件 hbox.getChildren().addAll(btn,text1,sld_time,text3,text2,sld_vol,text4); // BorderPane BorderPane pane = new BorderPane(); pane.setCenter(mv); pane.setBottom(hbox); // Scene Scene scene = new Scene(pane,650,500); mv.fitHeightProperty().bind(pane.heightProperty()); mv.fitWidthProperty().bind(pane.widthProperty()); // 标题 stage.setTitle("中国新说唱第九期"); // 场景 stage.setScene(scene); stage.show(); // 播放 mp.play(); // 鼠标事件处理器,处理鼠标点击和鼠标拖动事件 EventHandler<MouseEvent> action1 = new EventHandler<MouseEvent>(){ @Override public void handle(MouseEvent e){ double d = sld_time.getValue(); mp.seek(Duration.seconds(sld_time.getValue())); Integer a = new Integer((int)Math.floor(mp.getCurrentTime().toSeconds())/60); Integer b = new Integer((int)Math.floor(mp.getCurrentTime().toSeconds())%60/10); Integer c = new Integer((int)Math.floor(mp.getCurrentTime().toSeconds())%60%10); text3.setText("00:0"+a.toString()+":"+b.toString()+c.toString()+"/00:02:27"); } }; // 鼠标事件处理器,处理鼠标点击和鼠标拖动事件 EventHandler<MouseEvent> action2 = new EventHandler<MouseEvent>(){ @Override public void handle(MouseEvent e){ text4.setText(new Integer((int)sld_vol.getValue()).toString()); } }; // 注册 sld_time.setOnMouseDragged(action1); sld_time.setOnMouseClicked(action1); sld_vol.setOnMouseDragged(action2); sld_vol.setOnMouseClicked(action2); } // playButton方法,封装实现播放按钮的功能 public static Button playButton(MediaPlayer mp, Slider s){ Button btn = new Button("||"); btn.setOnMouseClicked(e->{ if(btn.getText().equals("||")){ btn.setText(">>"); mp.pause(); }else{ btn.setText("||"); mp.play(); } }); return btn; } // main()方法 public static void main(String[] args){ Application.launch(args); } }