From 2881c1f9d65dc3fea6bf7b06d7efa1799bd4f22a Mon Sep 17 00:00:00 2001 From: Jannik Date: Sun, 1 Apr 2018 23:24:49 +0200 Subject: [PATCH] added first version of the new player * added a javafx mediaplayer based player --- .../application/MainWindowController.java | 67 +++++++++-------- .../kellerkinder/HomeFlix/player/Player.java | 52 +++++++++++++ .../HomeFlix/player/PlayerController.java | 75 +++++++++++++++++++ src/main/resources/fxml/PlayerWindow.fxml | 26 +++++++ 4 files changed, 190 insertions(+), 30 deletions(-) create mode 100644 src/main/java/kellerkinder/HomeFlix/player/Player.java create mode 100644 src/main/java/kellerkinder/HomeFlix/player/PlayerController.java create mode 100644 src/main/resources/fxml/PlayerWindow.fxml diff --git a/src/main/java/kellerkinder/HomeFlix/application/MainWindowController.java b/src/main/java/kellerkinder/HomeFlix/application/MainWindowController.java index fe86e28..2c41075 100644 --- a/src/main/java/kellerkinder/HomeFlix/application/MainWindowController.java +++ b/src/main/java/kellerkinder/HomeFlix/application/MainWindowController.java @@ -97,6 +97,7 @@ import kellerkinder.HomeFlix.controller.DBController; import kellerkinder.HomeFlix.controller.OMDbAPIController; import kellerkinder.HomeFlix.controller.UpdateController; import kellerkinder.HomeFlix.datatypes.SourceDataType; +import kellerkinder.HomeFlix.player.Player; import kellerkinder.HomeFlix.datatypes.FilmTabelDataType; public class MainWindowController { @@ -228,6 +229,7 @@ public class MainWindowController { private boolean settingsTrue = false; private boolean autoUpdate = false; private boolean useBeta = false; + private boolean betaPlayer = false; private static final Logger LOGGER = LogManager.getLogger(MainWindowController.class.getName()); private int hashA = -647380320; @@ -545,42 +547,47 @@ public class MainWindowController { @FXML private void playbtnclicked() { // TODO rework when #19 is coming - - if (System.getProperty("os.name").contains("Linux")) { - String line; - String output = ""; - Process p; - try { - p = Runtime.getRuntime().exec("which vlc"); - BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); - while ((line = input.readLine()) != null) { - output = line; - } - LOGGER.info(output); - input.close(); - } catch (IOException e1) { - e1.printStackTrace(); - } - if (output.contains("which: no vlc") || output == "") { - JFXInfoAlert vlcInfoAlert = new JFXInfoAlert("Info", vlcNotInstalled, dialogBtnStyle, main.getPrimaryStage()); - vlcInfoAlert.showAndWait(); - } else { + + if (betaPlayer) { + new Player(streamUrl); + } else { + if (System.getProperty("os.name").contains("Linux")) { + String line; + String output = ""; + Process p; try { - Runtime.getRuntime().exec(new String[] { "vlc", streamUrl }); // TODO switch to ProcessBuilder + p = Runtime.getRuntime().exec("which vlc"); + BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); + while ((line = input.readLine()) != null) { + output = line; + } + LOGGER.info(output); + input.close(); + } catch (IOException e1) { + e1.printStackTrace(); + } + if (output.contains("which: no vlc") || output == "") { + JFXInfoAlert vlcInfoAlert = new JFXInfoAlert("Info", vlcNotInstalled, dialogBtnStyle, main.getPrimaryStage()); + vlcInfoAlert.showAndWait(); + } else { + try { + Runtime.getRuntime().exec(new String[] { "vlc", streamUrl }); // TODO switch to ProcessBuilder + } catch (IOException e) { + showErrorMsg(errorPlay, e); + } + } + + } else if (System.getProperty("os.name").contains("Windows") || System.getProperty("os.name").contains("Mac OS X")) { + try { + Desktop.getDesktop().open(new File(streamUrl)); } catch (IOException e) { showErrorMsg(errorPlay, e); } + } else { + LOGGER.error(System.getProperty("os.name") + ", OS is not supported, please contact a developer! "); } - - } else if (System.getProperty("os.name").contains("Windows") || System.getProperty("os.name").contains("Mac OS X")) { - try { - Desktop.getDesktop().open(new File(streamUrl)); - } catch (IOException e) { - showErrorMsg(errorPlay, e); - } - } else { - LOGGER.error(System.getProperty("os.name") + ", OS is not supported, please contact a developer! "); } + } @FXML diff --git a/src/main/java/kellerkinder/HomeFlix/player/Player.java b/src/main/java/kellerkinder/HomeFlix/player/Player.java new file mode 100644 index 0000000..e1cc12e --- /dev/null +++ b/src/main/java/kellerkinder/HomeFlix/player/Player.java @@ -0,0 +1,52 @@ +package kellerkinder.HomeFlix.player; + + +import javafx.event.EventHandler; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.scene.image.Image; +import javafx.scene.layout.AnchorPane; +import javafx.stage.Stage; +import javafx.stage.WindowEvent; +import kellerkinder.HomeFlix.application.Main; + +public class Player { + + private PlayerController playerController; + private Stage stage; + private AnchorPane pane; + + public Player(String file) { + try { + FXMLLoader fxmlLoader = new FXMLLoader(ClassLoader.getSystemResource("fxml/PlayerWindow.fxml")); + pane = (AnchorPane) fxmlLoader.load(); + stage = new Stage(); + stage.setScene(new Scene(pane)); + stage.setTitle("HomeFlix"); + stage.getIcons().add(new Image(Main.class.getResourceAsStream("/icons/Homeflix_Icon_64x64.png"))); + stage.setOnCloseRequest(new EventHandler() { + public void handle(WindowEvent we) { + playerController.getMediaPlayer().stop(); + } + }); + + playerController = fxmlLoader.getController(); + playerController.init(file, this); + + stage.setFullScreen(true); + stage.show(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public Stage getStage() { + return stage; + } + + public Parent getPane() { + return pane; + } + +} diff --git a/src/main/java/kellerkinder/HomeFlix/player/PlayerController.java b/src/main/java/kellerkinder/HomeFlix/player/PlayerController.java new file mode 100644 index 0000000..cc79eef --- /dev/null +++ b/src/main/java/kellerkinder/HomeFlix/player/PlayerController.java @@ -0,0 +1,75 @@ +package kellerkinder.HomeFlix.player; + +import java.io.File; + +import com.jfoenix.controls.JFXButton; + +import javafx.beans.binding.Bindings; +import javafx.beans.property.DoubleProperty; +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.scene.layout.HBox; +import javafx.scene.media.Media; +import javafx.scene.media.MediaPlayer; +import javafx.scene.media.MediaPlayer.Status; +import javafx.scene.media.MediaView; + +public class PlayerController { + + @FXML + private MediaView mediaView; + + @FXML + private HBox controllsHBox; + + @FXML + private JFXButton playBtn; + + @FXML + private JFXButton fullscreenBtn; + + private Player player; + private Media media; + private MediaPlayer mediaPlayer; + + public void init(String file, Player player) { + this.player = player; + + media = new Media(new File(file).toURI().toString()); + mediaPlayer = new MediaPlayer(media); + mediaView.setMediaPlayer(mediaPlayer); + + final DoubleProperty width = mediaView.fitWidthProperty(); + final DoubleProperty height = mediaView.fitHeightProperty(); + + width.bind(Bindings.selectDouble(mediaView.sceneProperty(), "width")); + height.bind(Bindings.selectDouble(mediaView.sceneProperty(), "height")); + + mediaView.setPreserveRatio(true); + mediaPlayer.play(); + } + + @FXML + void fullscreenBtnAction(ActionEvent event) { + if (player.getStage().isFullScreen()) { + player.getStage().setFullScreen(false); + } else { + player.getStage().setFullScreen(true); + } + } + + @FXML + void playBtnAction(ActionEvent event) { + + if (mediaPlayer.getStatus().equals(Status.PLAYING)) { + mediaPlayer.pause(); + } else { + mediaPlayer.play(); + } + } + + public MediaPlayer getMediaPlayer() { + return mediaPlayer; + } + +} diff --git a/src/main/resources/fxml/PlayerWindow.fxml b/src/main/resources/fxml/PlayerWindow.fxml new file mode 100644 index 0000000..8abe8a4 --- /dev/null +++ b/src/main/resources/fxml/PlayerWindow.fxml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + +