2018-04-02 18:29:59 +02:00
/ * *
* Project - HomeFlix
*
2020-04-01 19:48:44 +02:00
* Copyright 2016 - 2020 < seil0 @mosad.xyz >
2018-04-02 18:29:59 +02:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston ,
* MA 02110 - 1301 , USA .
*
* /
2019-01-08 17:10:33 +01:00
2020-04-22 18:29:39 +02:00
package org.mosad.homeflix.player ;
2018-04-01 23:24:49 +02:00
2019-06-22 21:04:43 +02:00
import java.awt.Desktop ;
import java.io.BufferedReader ;
import java.io.File ;
import java.io.IOException ;
import java.io.InputStreamReader ;
2019-06-17 00:44:44 +02:00
import java.net.URLConnection ;
2019-06-22 21:04:43 +02:00
import org.apache.logging.log4j.LogManager ;
import org.apache.logging.log4j.Logger ;
2020-04-22 18:29:39 +02:00
import org.mosad.homeflix.application.Main ;
2019-06-22 21:04:43 +02:00
2018-04-01 23:24:49 +02:00
import javafx.fxml.FXMLLoader ;
import javafx.scene.Scene ;
import javafx.scene.image.Image ;
import javafx.scene.layout.AnchorPane ;
2020-04-01 19:48:44 +02:00
import javafx.scene.layout.Pane ;
2018-04-01 23:24:49 +02:00
import javafx.stage.Stage ;
2020-04-01 19:48:44 +02:00
2018-04-01 23:24:49 +02:00
public class Player {
private PlayerController playerController ;
private Stage stage ;
private AnchorPane pane ;
2018-04-02 02:18:43 +02:00
private Scene scene ;
2019-06-22 21:04:43 +02:00
private static final Logger LOGGER = LogManager . getLogger ( Player . class . getName ( ) ) ;
2018-04-01 23:24:49 +02:00
2019-06-18 01:25:46 +02:00
// TODO move the players choose logic to a separate static class
2018-04-02 18:29:59 +02:00
/ * *
* generate a new PlayerWindow
2019-01-12 23:07:25 +01:00
* @param currentTableFilm the currently selected film
2018-04-02 18:29:59 +02:00
* /
2019-06-17 00:44:44 +02:00
public Player ( String streamURL ) {
2020-04-01 19:48:44 +02:00
try {
2020-05-22 16:46:46 +02:00
defaultPlayer ( streamURL ) ;
2020-04-01 19:48:44 +02:00
} catch ( Exception e ) {
LOGGER . error ( " Error while playing media " , e ) ;
2019-06-22 21:04:43 +02:00
legacyPlayer ( streamURL ) ;
}
}
/ * *
* start the integrated player
* @param streamURL
* /
2020-05-22 16:46:46 +02:00
private void defaultPlayer ( String mediaURL ) {
2020-04-01 19:48:44 +02:00
playerController = new PlayerController ( this , mediaURL ) ;
2018-12-08 23:44:17 +01:00
2018-04-01 23:24:49 +02:00
try {
2018-11-17 13:02:41 +01:00
FXMLLoader fxmlLoader = new FXMLLoader ( ) ;
fxmlLoader . setLocation ( getClass ( ) . getResource ( " /fxml/PlayerWindow.fxml " ) ) ;
2018-12-08 23:44:17 +01:00
fxmlLoader . setController ( playerController ) ;
2018-04-01 23:24:49 +02:00
pane = ( AnchorPane ) fxmlLoader . load ( ) ;
stage = new Stage ( ) ;
2018-04-02 02:18:43 +02:00
scene = new Scene ( pane ) ;
2020-07-09 19:42:26 +02:00
scene . getStylesheets ( ) . add ( getClass ( ) . getResource ( " /css/Player.css " ) . toExternalForm ( ) ) ;
2018-04-02 02:18:43 +02:00
stage . setScene ( scene ) ;
2018-04-01 23:24:49 +02:00
stage . setTitle ( " HomeFlix " ) ;
stage . getIcons ( ) . add ( new Image ( Main . class . getResourceAsStream ( " /icons/Homeflix_Icon_64x64.png " ) ) ) ;
2019-06-17 00:44:44 +02:00
stage . setOnCloseRequest ( event - > {
2020-04-03 16:03:52 +02:00
playerController . stop ( ) ;
2019-06-17 00:44:44 +02:00
stage . close ( ) ;
2018-04-01 23:24:49 +02:00
} ) ;
2020-04-01 19:48:44 +02:00
//stage.setFullScreen(true);
2018-04-01 23:24:49 +02:00
stage . show ( ) ;
2020-04-01 19:48:44 +02:00
2020-04-03 16:03:52 +02:00
playerController . init ( ) ;
playerController . start ( ) ;
2018-04-01 23:24:49 +02:00
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
2019-06-22 21:04:43 +02:00
/ * *
*
* /
private void legacyPlayer ( String streamURL ) {
LOGGER . warn ( " using fallback player! " ) ;
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 ( " which vlc: " + output ) ;
input . close ( ) ;
} catch ( IOException e1 ) {
e1 . printStackTrace ( ) ;
}
if ( output . contains ( " which: no vlc " ) | | output = = " " ) {
// JFXInfoAlert vlcInfoAlert = new JFXInfoAlert("Info",
// XMLController.getLocalBundle().getString("vlcNotInstalled"), btnStyle, primaryStage);
// vlcInfoAlert.showAndWait();
} else {
try {
new ProcessBuilder ( " vlc " , streamURL ) . start ( ) ;
} catch ( IOException e ) {
LOGGER . warn ( " An error has occurred while opening the file! " , 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 ) {
LOGGER . warn ( " An error has occurred while opening the file! " , e ) ;
}
} else {
LOGGER . error ( System . getProperty ( " os.name " ) + " , OS is not supported, please contact a developer! " ) ;
}
2018-04-02 02:18:43 +02:00
}
2019-06-17 00:44:44 +02:00
/ * *
* check if a film is supported by the HomeFlixPlayer or not this is the case if
* the mime type is mp4
*
2019-06-22 21:04:43 +02:00
* @param streamURL URL of the stream you want to check
2019-06-17 00:44:44 +02:00
* @return true if so , false if not
* /
public static boolean isSupportedFormat ( String streamURL ) {
String mimeType = URLConnection . guessContentTypeFromName ( streamURL ) ;
return mimeType ! = null & & ( mimeType . contains ( " mp4 " ) | | mimeType . contains ( " vp6 " ) ) ;
}
2019-06-22 21:04:43 +02:00
public Stage getStage ( ) {
return stage ;
}
public Scene getScene ( ) {
return scene ;
}
2019-06-17 00:44:44 +02:00
2020-04-01 19:48:44 +02:00
public Pane getPane ( ) {
return pane ;
}
2018-04-01 23:24:49 +02:00
}