Advertisement
Guest User

MediaView JavaFX

a guest
Aug 23rd, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.01 KB | None | 0 0
  1. package application;
  2.  
  3. import java.io.File;
  4. import java.net.URISyntaxException;
  5.  
  6. import javafx.application.Application;
  7. import javafx.application.Platform;
  8. import javafx.beans.InvalidationListener;
  9. import javafx.beans.Observable;
  10. import javafx.event.ActionEvent;
  11. import javafx.event.EventHandler;
  12. import javafx.geometry.Insets;
  13. import javafx.geometry.Pos;
  14. import javafx.scene.Group;
  15. import javafx.scene.Scene;
  16. import javafx.scene.control.Button;
  17. import javafx.scene.control.Label;
  18. import javafx.scene.control.Slider;
  19. import javafx.scene.layout.BorderPane;
  20. import javafx.scene.layout.HBox;
  21. import javafx.scene.layout.Pane;
  22. import javafx.scene.layout.Priority;
  23. import javafx.scene.layout.Region;
  24. import javafx.scene.media.Media;
  25. import javafx.scene.media.MediaPlayer;
  26. import javafx.scene.media.MediaPlayer.Status;
  27. import javafx.scene.media.MediaView;
  28. import javafx.stage.Stage;
  29. import javafx.util.Duration;
  30.  
  31. class MediaControl extends BorderPane {
  32.   private MediaPlayer mp;
  33.   private MediaView mediaView;
  34.   private final boolean repeat = false;
  35.   private boolean stopRequested = false;
  36.   private boolean atEndOfMedia = false;
  37.   private Duration duration;
  38.   private Slider timeSlider;
  39.   private Label playTime;
  40.   private Slider volumeSlider;
  41.   private HBox mediaBar;
  42.  
  43.   public MediaControl(final MediaPlayer mp) {
  44.     this.mp = mp;
  45.     setStyle("-fx-background-color: #bfc2c7;");
  46.     mediaView = new MediaView(mp);
  47.     Pane mvPane = new Pane() {
  48.     };
  49.     mvPane.getChildren().add(mediaView);
  50.     mvPane.setStyle("-fx-background-color: black;");
  51.     setCenter(mvPane);
  52.     mediaBar = new HBox();
  53.     mediaBar.setAlignment(Pos.CENTER);
  54.     mediaBar.setPadding(new Insets(5, 10, 5, 10));
  55.     BorderPane.setAlignment(mediaBar, Pos.CENTER);
  56.  
  57.     final Button playButton = new Button(">");
  58.  
  59.     playButton.setOnAction(new EventHandler<ActionEvent>() {
  60.       public void handle(ActionEvent e) {
  61.         Status status = mp.getStatus();
  62.  
  63.         if (status == Status.UNKNOWN || status == Status.HALTED) {
  64.           // don't do anything in these states
  65.           return;
  66.         }
  67.  
  68.         if (status == Status.PAUSED || status == Status.READY
  69.             || status == Status.STOPPED) {
  70.           // rewind the movie if we're sitting at the end
  71.           if (atEndOfMedia) {
  72.             mp.seek(mp.getStartTime());
  73.             atEndOfMedia = false;
  74.           }
  75.           mp.play();
  76.         } else {
  77.           mp.pause();
  78.         }
  79.       }
  80.     });
  81.     mp.currentTimeProperty().addListener(new InvalidationListener() {
  82.       public void invalidated(Observable ov) {
  83.         updateValues();
  84.       }
  85.     });
  86.  
  87.     mp.setOnPlaying(new Runnable() {
  88.       public void run() {
  89.         if (stopRequested) {
  90.           mp.pause();
  91.           stopRequested = false;
  92.         } else {
  93.           playButton.setText("||");
  94.         }
  95.       }
  96.     });
  97.  
  98.     mp.setOnPaused(new Runnable() {
  99.       public void run() {
  100.         System.out.println("onPaused");
  101.         playButton.setText(">");
  102.       }
  103.     });
  104.  
  105.     mp.setOnReady(new Runnable() {
  106.       public void run() {
  107.         duration = mp.getMedia().getDuration();
  108.         updateValues();
  109.       }
  110.     });
  111.  
  112.     mp.setCycleCount(repeat ? MediaPlayer.INDEFINITE : 1);
  113.     mp.setOnEndOfMedia(new Runnable() {
  114.       public void run() {
  115.         if (!repeat) {
  116.           playButton.setText(">");
  117.           stopRequested = true;
  118.           atEndOfMedia = true;
  119.         }
  120.       }
  121.     });
  122.     mediaBar.getChildren().add(playButton);
  123.     // Add spacer
  124.     Label spacer = new Label("   ");
  125.     mediaBar.getChildren().add(spacer);
  126.  
  127.     // Add Time label
  128.     Label timeLabel = new Label("Time: ");
  129.     mediaBar.getChildren().add(timeLabel);
  130.  
  131.     // Add time slider
  132.     timeSlider = new Slider();
  133.     HBox.setHgrow(timeSlider, Priority.ALWAYS);
  134.     timeSlider.setMinWidth(50);
  135.     timeSlider.setMaxWidth(Double.MAX_VALUE);
  136.  
  137.     timeSlider.valueProperty().addListener(new InvalidationListener() {
  138.       public void invalidated(Observable ov) {
  139.         if (timeSlider.isValueChanging()) {
  140.           // multiply duration by percentage calculated by slider position
  141.           mp.seek(duration.multiply(timeSlider.getValue() / 100.0));
  142.         }
  143.       }
  144.     });
  145.  
  146.     mediaBar.getChildren().add(timeSlider);
  147.  
  148.     // Add Play label
  149.     playTime = new Label();
  150.     playTime.setPrefWidth(130);
  151.     playTime.setMinWidth(50);
  152.     mediaBar.getChildren().add(playTime);
  153.  
  154.     // Add the volume label
  155.     Label volumeLabel = new Label("Vol: ");
  156.     mediaBar.getChildren().add(volumeLabel);
  157.  
  158.     // Add Volume slider
  159.     volumeSlider = new Slider();
  160.     volumeSlider.setPrefWidth(70);
  161.     volumeSlider.setMaxWidth(Region.USE_PREF_SIZE);
  162.     volumeSlider.setMinWidth(30);
  163.     volumeSlider.valueProperty().addListener(new InvalidationListener() {
  164.       public void invalidated(Observable ov) {
  165.         if (volumeSlider.isValueChanging()) {
  166.           mp.setVolume(volumeSlider.getValue() / 100.0);
  167.         }
  168.       }
  169.     });
  170.     mediaBar.getChildren().add(volumeSlider);
  171.     setBottom(mediaBar);
  172.   }
  173.  
  174.   protected void updateValues() {
  175.     if (playTime != null && timeSlider != null && volumeSlider != null) {
  176.       Platform.runLater(new Runnable() {
  177.         public void run() {
  178.           Duration currentTime = mp.getCurrentTime();
  179.           playTime.setText(formatTime(currentTime, duration));
  180.           timeSlider.setDisable(duration.isUnknown());
  181.           if (!timeSlider.isDisabled() && duration.greaterThan(Duration.ZERO)
  182.               && !timeSlider.isValueChanging()) {
  183.             timeSlider
  184.                 .setValue(currentTime.divide(duration).toMillis() * 100.0);
  185.           }
  186.           if (!volumeSlider.isValueChanging()) {
  187.             volumeSlider.setValue((int) Math.round(mp.getVolume() * 100));
  188.           }
  189.         }
  190.       });
  191.     }
  192.   }
  193.  
  194.   private static String formatTime(Duration elapsed, Duration duration) {
  195.     int intElapsed = (int) Math.floor(elapsed.toSeconds());
  196.     int elapsedHours = intElapsed / (60 * 60);
  197.     if (elapsedHours > 0) {
  198.       intElapsed -= elapsedHours * 60 * 60;
  199.     }
  200.     int elapsedMinutes = intElapsed / 60;
  201.     int elapsedSeconds = intElapsed - elapsedHours * 60 * 60 - elapsedMinutes
  202.         * 60;
  203.  
  204.     if (duration.greaterThan(Duration.ZERO)) {
  205.       int intDuration = (int) Math.floor(duration.toSeconds());
  206.       int durationHours = intDuration / (60 * 60);
  207.       if (durationHours > 0) {
  208.         intDuration -= durationHours * 60 * 60;
  209.       }
  210.       int durationMinutes = intDuration / 60;
  211.       int durationSeconds = intDuration - durationHours * 60 * 60
  212.           - durationMinutes * 60;
  213.       if (durationHours > 0) {
  214.         return String.format("%d:%02d:%02d/%d:%02d:%02d", elapsedHours,
  215.             elapsedMinutes, elapsedSeconds, durationHours, durationMinutes,
  216.             durationSeconds);
  217.       } else {
  218.         return String.format("%02d:%02d/%02d:%02d", elapsedMinutes,
  219.             elapsedSeconds, durationMinutes, durationSeconds);
  220.       }
  221.     } else {
  222.       if (elapsedHours > 0) {
  223.         return String.format("%d:%02d:%02d", elapsedHours, elapsedMinutes,
  224.             elapsedSeconds);
  225.       } else {
  226.         return String.format("%02d:%02d", elapsedMinutes, elapsedSeconds);
  227.       }
  228.     }
  229.   }
  230. }
  231.  
  232. /**
  233.  *
  234.  * @author cmcastil
  235.  */
  236. public class Main extends Application {
  237.  
  238.   /**
  239.    * @param args
  240.    *          the command line arguments
  241.    */
  242.   public static void main(String[] args) {
  243.     launch(args);
  244.   }
  245.  
  246.   @Override
  247.   public void start(Stage primaryStage) throws URISyntaxException {
  248.     primaryStage.setTitle("Hello Media!");
  249.     Group root = new Group();
  250.     Scene scene = new Scene(root, 540, 209);
  251.  
  252.     // create media player
  253.     Media media = new Media("file:///home/user/sample.mp4");
  254.     System.out.println("The Source--> " + media.getSource());
  255.     MediaPlayer mediaPlayer = new MediaPlayer(media);
  256.     mediaPlayer.play();
  257.  
  258.     MediaControl mediaControl = new MediaControl(mediaPlayer);
  259.     scene.setRoot(mediaControl);
  260.  
  261.     primaryStage.setScene(scene);
  262.     primaryStage.show();
  263.   }
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement