Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.43 KB | None | 0 0
  1. /**
  2. * Copyright (c) 2008, 2012 Oracle and/or its affiliates.
  3. * All rights reserved. Use is subject to license terms.
  4. */
  5. import java.io.File;
  6. import java.net.URI;
  7.  
  8. import javafx.application.Application;
  9. import javafx.scene.Group;
  10. import javafx.scene.Scene;
  11. import javafx.stage.Stage;
  12. import javafx.application.Platform;
  13. import javafx.beans.InvalidationListener;
  14. import javafx.beans.Observable;
  15. import javafx.event.ActionEvent;
  16. import javafx.event.EventHandler;
  17. import javafx.geometry.Insets;
  18. import javafx.geometry.Pos;
  19. import javafx.scene.control.Button;
  20. import javafx.scene.control.Label;
  21. import javafx.scene.control.Slider;
  22. import javafx.scene.layout.BorderPane;
  23. import javafx.scene.layout.HBox;
  24. import javafx.scene.layout.Pane;
  25. import javafx.scene.layout.Priority;
  26. import javafx.scene.layout.Region;
  27. import javafx.scene.media.Media;
  28. import javafx.scene.media.MediaPlayer;
  29. import javafx.scene.media.MediaPlayer.Status;
  30. import javafx.scene.media.MediaView;
  31. import javafx.util.Duration;
  32. import javafx.beans.value.ChangeListener;
  33. import javafx.beans.value.ObservableValue;
  34. import javafx.scene.image.Image;
  35. import javafx.scene.image.ImageView;
  36. import javafx.scene.Scene;
  37. import javafx.scene.control.*;
  38. import javafx.stage.Stage;
  39.  
  40. /**
  41. * An advanced media player with controls for play/pause, seek, and volume.
  42. *
  43. * @see javafx.scene.media.MediaPlayer
  44. * @see javafx.scene.media.Media
  45. */
  46. public class AdvancedMedia extends Application {
  47. private static final String MEDIA_URL =// "file:///D:/sri1/test.mp4";
  48. //private static final String MEDIA_URL = "http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv";
  49. "http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv";
  50. private MediaPlayer mediaPlayer;
  51. private AdvancedMedia.MediaControl mediaControl;
  52.  
  53. private void init(Stage primaryStage) {
  54. Group root = new Group();
  55. primaryStage.setScene(new Scene(root));
  56.  
  57.  
  58. mediaPlayer = new MediaPlayer(new Media(MEDIA_URL));
  59. mediaPlayer.setAutoPlay(true);
  60. mediaControl = new AdvancedMedia.MediaControl(mediaPlayer);
  61. mediaControl.setMinSize(480,280);
  62. mediaControl.setPrefSize(480,280);
  63. mediaControl.setMaxSize(480,280);
  64. root.getChildren().add(mediaControl);
  65. }
  66.  
  67. public void play() {
  68. Status status = mediaPlayer.getStatus();
  69. if (status == Status.UNKNOWN
  70. || status == Status.HALTED)
  71. {
  72. //System.out.println("Player is in a bad or unknown state, can't play.");
  73. return;
  74. }
  75.  
  76. if (status == Status.PAUSED
  77. || status == Status.STOPPED
  78. || status == Status.READY)
  79. {
  80. mediaPlayer.play();
  81. }
  82. }
  83.  
  84. @Override public void stop() {
  85. mediaPlayer.stop();
  86. }
  87.  
  88. public class MediaControl extends BorderPane {
  89. private MediaPlayer mp;
  90. private MediaView mediaView;
  91. private final boolean repeat = false;
  92. private boolean stopRequested = false;
  93. private boolean atEndOfMedia = false;
  94. private Duration duration;
  95. private Slider timeSlider;
  96. private Label playTime;
  97. private Slider volumeSlider;
  98. private HBox mediaBar;
  99. private final Image PlayButtonImage = new Image("file:///D:/sri/UpgradeReport_Plus.gif");
  100. ImageView imageViewPlay = new ImageView(PlayButtonImage);
  101. //URI x=new URI("file:///D:/deepak/DEEP PHOTO.jpg");
  102. File f=new File("file:///D:/sri/UpgradeReport_Plus.gif");
  103. URI u=f.toURI();
  104. private final Image PauseButtonImage = new Image(u.toString());
  105.  
  106. ImageView imageViewPause = new ImageView(PauseButtonImage);
  107. private Pane mvPane;
  108. private Stage newStage;
  109. private boolean fullScreen = false;
  110.  
  111. @Override protected void layoutChildren() {
  112. if (mediaView != null && getBottom() != null) {
  113. mediaView.setFitWidth(getWidth());
  114. mediaView.setFitHeight(getHeight() - getBottom().prefHeight(-1));
  115. }
  116. super.layoutChildren();
  117. if (mediaView != null && getCenter() != null) {
  118. mediaView.setTranslateX((((Pane)getCenter()).getWidth() - mediaView.prefWidth(-1)) / 2);
  119. mediaView.setTranslateY((((Pane)getCenter()).getHeight() - mediaView.prefHeight(-1)) / 2);
  120. }
  121. }
  122.  
  123. @Override protected double computeMinWidth(double height) {
  124. return mediaBar.prefWidth(-1);
  125. }
  126.  
  127. @Override protected double computeMinHeight(double width) {
  128. return 200;
  129. }
  130.  
  131. @Override protected double computePrefWidth(double height) {
  132. return Math.max(mp.getMedia().getWidth(), mediaBar.prefWidth(height));
  133. }
  134.  
  135. @Override protected double computePrefHeight(double width) {
  136. return mp.getMedia().getHeight() + mediaBar.prefHeight(width);
  137. }
  138.  
  139. @Override protected double computeMaxWidth(double height) { return Double.MAX_VALUE; }
  140.  
  141. @Override protected double computeMaxHeight(double width) { return Double.MAX_VALUE; }
  142.  
  143. public MediaControl(final MediaPlayer mp) {
  144. this.mp=mp;
  145. setStyle("-fx-background-color: #bfc2c7;"); // TODO: Use css file
  146. mediaView = new MediaView(mp);
  147. mvPane = new Pane();
  148. mvPane.getChildren().add(mediaView);
  149. mvPane.setStyle("-fx-background-color: black;"); // TODO: Use css file
  150. setCenter(mvPane);
  151. mediaBar = new HBox(5.0);
  152. mediaBar.setPadding(new Insets(5, 10, 5, 10));
  153. mediaBar.setAlignment(Pos.CENTER_LEFT);
  154. BorderPane.setAlignment(mediaBar, Pos.CENTER);
  155.  
  156. final Button playButton = ButtonBuilder.create()
  157. .minWidth(Control.USE_PREF_SIZE)
  158. .build();
  159.  
  160. playButton.setGraphic(imageViewPlay);
  161. playButton.setOnAction(new EventHandler<ActionEvent>() {
  162. public void handle(ActionEvent e) {
  163. updateValues();
  164. Status status = mp.getStatus();
  165. if (status == Status.UNKNOWN
  166. || status == Status.HALTED)
  167. {
  168. // don't do anything in these states
  169. return;
  170. }
  171.  
  172. if (status == Status.PAUSED
  173. || status == Status.READY
  174. || status == Status.STOPPED)
  175. {
  176. // rewind the movie if we're sitting at the end
  177. if (atEndOfMedia) {
  178. mp.seek(mp.getStartTime());
  179. atEndOfMedia = false;
  180. playButton.setGraphic(imageViewPlay);
  181. //playButton.setText(">");
  182. updateValues();
  183. }
  184. mp.play();
  185. playButton.setGraphic(imageViewPause);
  186. //playButton.setText("||");
  187. }
  188. else {
  189. mp.pause();
  190. }
  191. }
  192. });
  193. mp.currentTimeProperty().addListener(new ChangeListener<Duration>() {
  194. @Override
  195. public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) {
  196. updateValues();
  197. }
  198. });
  199. mp.setOnPlaying(new Runnable() {
  200. public void run() {
  201.  
  202. if (stopRequested) {
  203. mp.pause();
  204. stopRequested = false;
  205. } else {
  206. playButton.setGraphic(imageViewPause);
  207. //playButton.setText("||");
  208. }
  209. }
  210. });
  211. mp.setOnPaused(new Runnable() {
  212. public void run() {
  213.  
  214. playButton.setGraphic(imageViewPlay);
  215. //playButton.setText("||");
  216. }
  217. });
  218. mp.setOnReady(new Runnable() {
  219. public void run() {
  220. duration = mp.getMedia().getDuration();
  221. updateValues();
  222. }
  223. });
  224.  
  225. mp.setCycleCount(repeat ? MediaPlayer.INDEFINITE : 1);
  226. mp.setOnEndOfMedia(new Runnable() {
  227. public void run() {
  228. if (!repeat) {
  229. playButton.setGraphic(imageViewPlay);
  230. //playButton.setText(">");
  231. stopRequested = true;
  232. atEndOfMedia = true;
  233. }
  234. }
  235. });
  236. mediaBar.getChildren().add(playButton);
  237.  
  238. // Time label
  239. Label timeLabel = new Label("Time");
  240. timeLabel.setMinWidth(Control.USE_PREF_SIZE);
  241. mediaBar.getChildren().add(timeLabel);
  242.  
  243.  
  244. // Time slider
  245. timeSlider = SliderBuilder.create()
  246. .minWidth(30)
  247. .maxWidth(Double.MAX_VALUE)
  248. .build();
  249. HBox.setHgrow(timeSlider, Priority.ALWAYS);
  250. timeSlider.valueProperty().addListener(new InvalidationListener() {
  251. public void invalidated(Observable ov) {
  252. if (timeSlider.isValueChanging()) {
  253. // multiply duration by percentage calculated by slider position
  254. if(duration!=null) {
  255. mp.seek(duration.multiply(timeSlider.getValue() / 100.0));
  256. }
  257. updateValues();
  258.  
  259. }
  260. }
  261. });
  262. mediaBar.getChildren().add(timeSlider);
  263.  
  264. // Play label
  265. playTime = LabelBuilder.create()
  266. //.prefWidth(130)
  267. .minWidth(Control.USE_PREF_SIZE)
  268. .build();
  269.  
  270. mediaBar.getChildren().add(playTime);
  271.  
  272.  
  273. //Fullscreen button
  274.  
  275. Button buttonFullScreen = ButtonBuilder.create()
  276. .text("Full Screen")
  277. .minWidth(Control.USE_PREF_SIZE)
  278. .build();
  279.  
  280. buttonFullScreen.setOnAction(new EventHandler<ActionEvent>() {
  281. @Override
  282. public void handle(ActionEvent event) {
  283. if (!fullScreen){
  284. newStage = new Stage();
  285. newStage.fullScreenProperty().addListener(new ChangeListener<Boolean>() {
  286. @Override public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
  287. onFullScreen();
  288. }
  289. });
  290. final BorderPane borderPane = new BorderPane(){
  291. @Override protected void layoutChildren(){
  292. if (mediaView != null && getBottom() != null) {
  293. mediaView.setFitWidth(getWidth());
  294. mediaView.setFitHeight(getHeight() - getBottom().prefHeight(-1));
  295. }
  296. super.layoutChildren();
  297. if (mediaView != null) {
  298. mediaView.setTranslateX((((Pane)getCenter()).getWidth() - mediaView.prefWidth(-1)) / 2);
  299. mediaView.setTranslateY((((Pane)getCenter()).getHeight() - mediaView.prefHeight(-1)) / 2);
  300. }
  301. };
  302. };
  303.  
  304. setCenter(null);
  305. setBottom(null);
  306. borderPane.setCenter(mvPane);
  307. borderPane.setBottom(mediaBar);
  308.  
  309. Scene newScene = new Scene(borderPane);
  310. newStage.setScene(newScene);
  311. //Workaround for disposing stage when exit fullscreen
  312. newStage.setX(-100000);
  313. newStage.setY(-100000);
  314.  
  315. newStage.setFullScreen(true);
  316. fullScreen = true;
  317. newStage.show();
  318.  
  319. }
  320. else{
  321. //toggle FullScreen
  322. fullScreen = false;
  323. newStage.setFullScreen(false);
  324.  
  325. }
  326. }
  327.  
  328. });
  329. mediaBar.getChildren().add(buttonFullScreen);
  330.  
  331. // Volume label
  332. Label volumeLabel = new Label("Vol");
  333. volumeLabel.setMinWidth(Control.USE_PREF_SIZE);
  334. mediaBar.getChildren().add(volumeLabel);
  335.  
  336. // Volume slider
  337. volumeSlider = SliderBuilder.create()
  338. .prefWidth(70)
  339. .minWidth(30)
  340. .maxWidth(Region.USE_PREF_SIZE)
  341. .build();
  342. volumeSlider.valueProperty().addListener(new InvalidationListener() {
  343. public void invalidated(Observable ov) {
  344. }
  345. });
  346. volumeSlider.valueProperty().addListener(new ChangeListener<Number>() {
  347. @Override
  348. public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
  349. if (volumeSlider.isValueChanging()) {
  350. mp.setVolume(volumeSlider.getValue() / 100.0);
  351. }
  352. }
  353. });
  354. mediaBar.getChildren().add(volumeSlider);
  355.  
  356. setBottom(mediaBar);
  357.  
  358. }
  359.  
  360. protected void onFullScreen(){
  361. if (!newStage.isFullScreen()){
  362.  
  363. fullScreen = false;
  364. setCenter(mvPane);
  365. setBottom(mediaBar);
  366. Platform.runLater(new Runnable() {
  367. @Override public void run() {
  368. newStage.close();
  369. }
  370. });
  371.  
  372. }
  373. }
  374.  
  375. protected void updateValues() {
  376. if (playTime != null && timeSlider != null && volumeSlider != null && duration != null) {
  377. Platform.runLater(new Runnable() {
  378. public void run() {
  379. Duration currentTime = mp.getCurrentTime();
  380. playTime.setText(formatTime(currentTime, duration));
  381. timeSlider.setDisable(duration.isUnknown());
  382. if (!timeSlider.isDisabled() && duration.greaterThan(Duration.ZERO) && !timeSlider.isValueChanging()) {
  383. timeSlider.setValue(currentTime.divide(duration).toMillis() * 100.0);
  384. }
  385. if (!volumeSlider.isValueChanging()) {
  386. volumeSlider.setValue((int) Math.round(mp.getVolume() * 100));
  387. }
  388. }
  389. });
  390. }
  391. }
  392.  
  393.  
  394. private String formatTime(Duration elapsed, Duration duration) {
  395. int intElapsed = (int)Math.floor(elapsed.toSeconds());
  396. int elapsedHours = intElapsed / (60 * 60);
  397. if (elapsedHours > 0) {
  398. intElapsed -= elapsedHours * 60 * 60;
  399. }
  400. int elapsedMinutes = intElapsed / 60;
  401. int elapsedSeconds = intElapsed - elapsedHours * 60 * 60 - elapsedMinutes * 60;
  402.  
  403. if (duration.greaterThan(Duration.ZERO)) {
  404. int intDuration = (int)Math.floor(duration.toSeconds());
  405. int durationHours = intDuration / (60 * 60);
  406. if (durationHours > 0) {
  407. intDuration -= durationHours * 60 * 60;
  408. }
  409. int durationMinutes = intDuration / 60;
  410. int durationSeconds = intDuration - durationHours * 60 * 60 - durationMinutes * 60;
  411.  
  412. if (durationHours > 0) {
  413. return String.format("%d:%02d:%02d/%d:%02d:%02d",
  414. elapsedHours, elapsedMinutes, elapsedSeconds,
  415. durationHours, durationMinutes, durationSeconds);
  416. } else {
  417. return String.format("%02d:%02d/%02d:%02d",
  418. elapsedMinutes, elapsedSeconds,
  419. durationMinutes, durationSeconds);
  420. }
  421. } else {
  422. if (elapsedHours > 0) {
  423. return String.format("%d:%02d:%02d",
  424. elapsedHours, elapsedMinutes, elapsedSeconds);
  425. } else {
  426. return String.format("%02d:%02d",
  427. elapsedMinutes, elapsedSeconds);
  428. }
  429. }
  430. }
  431. }
  432.  
  433. @Override public void start(Stage primaryStage) throws Exception {
  434. init(primaryStage);
  435. primaryStage.show();
  436. play();
  437. }
  438. public static void main(String[] args) { launch(args); }
  439. }
  440.  
  441. private static File file = new File("d:/videos/xyz.mp4");
  442. private static final String MEDIA_URL = file.toURI().toString();
  443.  
  444. import java.io.File;
  445. import javafx.application.Application;
  446. import javafx.beans.binding.Bindings;
  447. import javafx.beans.property.DoubleProperty;
  448. import javafx.scene.Scene;
  449. import javafx.scene.layout.StackPane;
  450. import javafx.scene.media.Media;
  451. import javafx.scene.media.MediaView;
  452. import javafx.scene.paint.Color;
  453. import javafx.stage.Stage;
  454.  
  455. public class Video extends Application{
  456. private String Dir = System.getProperty("user.dir");
  457. public static void main(String[] args) throws Exception{
  458. launch(args);
  459. }
  460.  
  461. @Override
  462. public void start(Stage stage) throws Exception {
  463.  
  464. //goes to user Directory
  465. File f = new File(Dir, "Epic Lightsaber Duel - Star Wars_ The Force Awakens.mp4");
  466.  
  467.  
  468. //Converts media to string URL
  469. Media media = new Media(f.toURI().toURL().toString());
  470. javafx.scene.media.MediaPlayer player = new javafx.scene.media.MediaPlayer(media);
  471. MediaView viewer = new MediaView(player);
  472.  
  473. //change width and height to fit video
  474. DoubleProperty width = viewer.fitWidthProperty();
  475. DoubleProperty height = viewer.fitHeightProperty();
  476. width.bind(Bindings.selectDouble(viewer.sceneProperty(), "width"));
  477. height.bind(Bindings.selectDouble(viewer.sceneProperty(), "height"));
  478. viewer.setPreserveRatio(true);
  479.  
  480.  
  481. StackPane root = new StackPane();
  482. root.getChildren().add(viewer);
  483.  
  484. //set the Scene
  485. Scene scenes = new Scene(root, 500, 500, Color.BLACK);
  486. stage.setScene(scenes);
  487. stage.setTitle("Riddle Game");
  488. stage.setFullScreen(true);
  489. stage.show();
  490. player.play();
  491. }
  492. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement