2018-04-02 18:29:59 +02:00
/ * *
* Project - HomeFlix
*
* Copyright 2016 - 2018 < @Seil0 >
*
* 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 .
*
* /
2018-04-01 23:24:49 +02:00
package kellerkinder.HomeFlix.player ;
import java.io.File ;
2018-04-02 02:18:43 +02:00
import java.util.Timer ;
import java.util.TimerTask ;
2018-04-01 23:24:49 +02:00
import com.jfoenix.controls.JFXButton ;
2018-04-02 02:18:43 +02:00
import com.jfoenix.controls.JFXSlider ;
2018-04-01 23:24:49 +02:00
import javafx.beans.binding.Bindings ;
import javafx.beans.property.DoubleProperty ;
2018-04-02 02:18:43 +02:00
import javafx.beans.value.ChangeListener ;
import javafx.beans.value.ObservableValue ;
2018-04-01 23:24:49 +02:00
import javafx.event.ActionEvent ;
2018-04-02 02:18:43 +02:00
import javafx.event.EventHandler ;
2018-04-01 23:24:49 +02:00
import javafx.fxml.FXML ;
2018-04-02 02:18:43 +02:00
import javafx.scene.Cursor ;
import javafx.scene.image.Image ;
import javafx.scene.image.ImageView ;
import javafx.scene.input.MouseEvent ;
2018-04-01 23:24:49 +02:00
import javafx.scene.layout.HBox ;
2018-04-02 02:18:43 +02:00
import javafx.scene.layout.VBox ;
2018-04-01 23:24:49 +02:00
import javafx.scene.media.Media ;
import javafx.scene.media.MediaPlayer ;
import javafx.scene.media.MediaPlayer.Status ;
import javafx.scene.media.MediaView ;
2018-04-02 02:18:43 +02:00
import javafx.util.Duration ;
import kellerkinder.HomeFlix.controller.DBController ;
2018-04-01 23:24:49 +02:00
public class PlayerController {
@FXML
private MediaView mediaView ;
2018-04-02 02:18:43 +02:00
@FXML
private VBox bottomVBox ;
2018-04-01 23:24:49 +02:00
@FXML
2018-04-02 02:18:43 +02:00
private HBox controlsHBox ;
2018-04-01 23:24:49 +02:00
2018-04-02 02:18:43 +02:00
@FXML
private JFXSlider timeSlider ;
@FXML
private JFXButton stopBtn ;
2018-04-01 23:24:49 +02:00
@FXML
private JFXButton playBtn ;
@FXML
private JFXButton fullscreenBtn ;
private Player player ;
2018-04-02 02:18:43 +02:00
private DBController dbController ;
2018-04-01 23:24:49 +02:00
private Media media ;
private MediaPlayer mediaPlayer ;
2018-04-02 02:18:43 +02:00
private double currentTime = 0 ;
private double futureTime = 0 ;
private double duration = 0 ;
private boolean mousePressed = false ;
private boolean showControls = true ;
private String file ;
private int nextEp ;
private ImageView stop_black = new ImageView ( new Image ( " icons/ic_stop_black_24dp_1x.png " ) ) ;
private ImageView play_arrow_black = new ImageView ( new Image ( " icons/ic_play_arrow_black_24dp_1x.png " ) ) ;
private ImageView pause_black = new ImageView ( new Image ( " icons/ic_pause_black_24dp_1x.png " ) ) ;
private ImageView fullscreen_black = new ImageView ( new Image ( " icons/ic_fullscreen_black_24dp_1x.png " ) ) ;
private ImageView fullscreen_exit_black = new ImageView ( new Image ( " icons/ic_fullscreen_exit_black_24dp_1x.png " ) ) ;
2018-04-02 18:29:59 +02:00
/ * *
* initialize the new PlayerWindow
* @param file the file you want to play
* @param currentEp the current episode ( needed for autoplay )
* @param player the player object ( needed for closing action )
* @param dbController the dbController object
* /
2018-04-02 02:18:43 +02:00
public void init ( String file , String currentEp , Player player , DBController dbController ) {
this . file = file ;
2018-04-01 23:24:49 +02:00
this . player = player ;
2018-04-02 02:18:43 +02:00
this . dbController = dbController ;
initActions ( ) ;
if ( currentEp . length ( ) > 0 ) {
nextEp = Integer . parseInt ( currentEp ) + 1 ;
} else {
nextEp = 0 ;
}
2018-04-01 23:24:49 +02:00
media = new Media ( new File ( file ) . toURI ( ) . toString ( ) ) ;
mediaPlayer = new MediaPlayer ( media ) ;
2018-04-02 02:18:43 +02:00
mediaView . setPreserveRatio ( true ) ;
2018-04-01 23:24:49 +02:00
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 " ) ) ;
2018-04-02 02:18:43 +02:00
mediaPlayer . setOnReady ( new Runnable ( ) {
@Override
public void run ( ) {
duration = media . getDuration ( ) . toMillis ( ) ;
timeSlider . setMax ( ( duration / 1000 ) / 60 ) ;
mediaPlayer . setStartTime ( Duration . millis ( dbController . getCurrentTime ( file ) ) ) ;
mediaPlayer . play ( ) ;
}
} ) ;
mediaPlayer . currentTimeProperty ( ) . addListener ( new ChangeListener < Duration > ( ) {
@Override
public void changed ( ObservableValue < ? extends Duration > observable , Duration oldValue , Duration newValue ) {
currentTime = newValue . toMillis ( ) ;
if ( duration - currentTime < 10000 ) {
if ( nextEp ! = 0 ) {
2018-04-02 18:29:59 +02:00
dbController . getNextEpisode ( new File ( file ) . getName ( ) , nextEp ) ;
2018-04-02 02:18:43 +02:00
System . out . println ( " next episode is: " + dbController . getNextEpisode ( file , nextEp ) ) ;
} else {
if ( duration - currentTime < 100 ) {
dbController . setCurrentTime ( file , 0 ) ;
mediaPlayer . stop ( ) ;
player . getStage ( ) . close ( ) ;
}
}
}
if ( ! mousePressed ) {
timeSlider . setValue ( ( currentTime / 1000 ) / 60 ) ;
}
}
} ) ;
stopBtn . setGraphic ( stop_black ) ;
playBtn . setGraphic ( play_arrow_black ) ;
fullscreenBtn . setGraphic ( fullscreen_exit_black ) ;
}
2018-04-02 18:29:59 +02:00
/ * *
* initialize some PlayerWindow GUI - Elements actions
* /
2018-04-02 02:18:43 +02:00
private void initActions ( ) {
player . getScene ( ) . addEventFilter ( MouseEvent . MOUSE_MOVED , new EventHandler < MouseEvent > ( ) {
// hide controls timer init
final Timer timer = new Timer ( ) ;
TimerTask controlAnimationTask = null ; // task to execute save operation
final long delayTime = 5000 ;
@Override
public void handle ( MouseEvent mouseEvent ) {
// show controls
if ( ! showControls ) {
player . getScene ( ) . setCursor ( Cursor . DEFAULT ) ;
bottomVBox . setVisible ( true ) ;
}
// hide controls
if ( controlAnimationTask ! = null )
controlAnimationTask . cancel ( ) ;
controlAnimationTask = new TimerTask ( ) {
@Override
public void run ( ) {
bottomVBox . setVisible ( false ) ;
player . getScene ( ) . setCursor ( Cursor . NONE ) ;
showControls = false ;
}
} ;
timer . schedule ( controlAnimationTask , delayTime ) ;
}
} ) ;
timeSlider . setOnMouseReleased ( new EventHandler < MouseEvent > ( ) {
@Override
public void handle ( MouseEvent event ) {
mediaPlayer . seek ( new Duration ( futureTime ) ) ;
mousePressed = false ;
}
} ) ;
timeSlider . setOnMousePressed ( new EventHandler < MouseEvent > ( ) {
@Override
public void handle ( MouseEvent event ) {
mousePressed = true ;
}
} ) ;
timeSlider . valueProperty ( ) . addListener ( new ChangeListener < Number > ( ) {
@Override
public void changed ( ObservableValue < ? extends Number > ov , Number old_val , Number new_val ) {
futureTime = ( double ) new_val * 1000 * 60 ;
}
} ) ;
}
@FXML
void stopBtnAction ( ActionEvent event ) {
dbController . setCurrentTime ( file , currentTime ) ;
mediaPlayer . stop ( ) ;
player . getStage ( ) . close ( ) ;
2018-04-01 23:24:49 +02:00
}
@FXML
void fullscreenBtnAction ( ActionEvent event ) {
if ( player . getStage ( ) . isFullScreen ( ) ) {
player . getStage ( ) . setFullScreen ( false ) ;
2018-04-02 02:18:43 +02:00
fullscreenBtn . setGraphic ( fullscreen_black ) ;
2018-04-01 23:24:49 +02:00
} else {
player . getStage ( ) . setFullScreen ( true ) ;
2018-04-02 02:18:43 +02:00
fullscreenBtn . setGraphic ( fullscreen_exit_black ) ;
2018-04-01 23:24:49 +02:00
}
}
@FXML
void playBtnAction ( ActionEvent event ) {
if ( mediaPlayer . getStatus ( ) . equals ( Status . PLAYING ) ) {
mediaPlayer . pause ( ) ;
2018-04-02 02:18:43 +02:00
playBtn . setGraphic ( play_arrow_black ) ;
2018-04-01 23:24:49 +02:00
} else {
mediaPlayer . play ( ) ;
2018-04-02 02:18:43 +02:00
playBtn . setGraphic ( pause_black ) ;
2018-04-01 23:24:49 +02:00
}
}
public MediaPlayer getMediaPlayer ( ) {
return mediaPlayer ;
}
2018-04-02 02:18:43 +02:00
public double getCurrentTime ( ) {
return currentTime ;
}
2018-04-01 23:24:49 +02:00
}