import
java.io.File;
import
java.net.MalformedURLException;
import
javafx.application.Application;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.stage.FileChooser;
import
javafx.stage.Stage;
import
javafx.scene.Scene;
import
javafx.scene.control.Menu;
import
javafx.scene.control.MenuBar;
import
javafx.scene.control.MenuItem;
import
javafx.scene.layout.BorderPane;
import
javafx.scene.paint.Color;
public
class
Main
extends
Application {
Player player;
FileChooser fileChooser;
public
void
start(
final
Stage primaryStage)
{
MenuItem open =
new
MenuItem(
"Open"
);
Menu file =
new
Menu(
"File"
);
MenuBar menu =
new
MenuBar();
file.getItems().add(open);
menu.getMenus().add(file);
fileChooser =
new
FileChooser();
open.setOnAction(
new
EventHandler<ActionEvent>(){
public
void
handle(ActionEvent e)
{
player.player.pause();
File file = fileChooser.showOpenDialog(primaryStage);
if
(file !=
null
) {
try
{
player =
new
Player(file.toURI().toURL().toExternalForm());
Scene scene =
new
Scene(player,
720
,
535
, Color.BLACK);
primaryStage.setScene(scene);
}
catch
(MalformedURLException e1) {
e1.printStackTrace();
}
}
}
player =
new
Player(
"file:/// F:/songs/srk.mp4"
);
player.setTop(menu);
Scene scene =
new
Scene(player,
720
,
535
, Color.BLACK);
primaryStage.setScene(scene);
primaryStage.show();
}
public
static
void
main(String[] args){
launch(args);
}
}
import
javafx.scene.layout.BorderPane;
import
javafx.scene.layout.Pane;
import
javafx.scene.media.Media;
import
javafx.scene.media.MediaPlayer;
import
javafx.scene.media.MediaView;
public
class
Player
extends
BorderPane
{
Media media;
MediaPlayer player;
MediaView view;
Pane mpane;
MediaBar bar;
public
Player(String file)
{
media =
new
Media(file);
player =
new
MediaPlayer(media);
view =
new
MediaView(player);
mpane =
new
Pane();
mpane.getChildren().add(view);
setCenter(mpane);
bar =
new
MediaBar(player);
setBottom(bar);
setStyle(
"-fx-background-color:#bfc2c7"
);
player.play();
}
}
import
javafx.application.Platform;
import
javafx.beans.InvalidationListener;
import
javafx.beans.Observable;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.geometry.Insets;
import
javafx.geometry.Pos;
import
javafx.scene.control.Button;
import
javafx.scene.control.Label;
import
javafx.scene.control.Slider;
import
javafx.scene.layout.HBox;
import
javafx.scene.layout.Priority;
import
javafx.scene.media.MediaPlayer;
import
javafx.scene.media.MediaPlayer.Status;
public
class
MediaBar
extends
HBox {
Slider time =
new
Slider();
Slider vol =
new
Slider();
Button PlayButton =
new
Button(
"||"
);
Label volume =
new
Label(
"Volume: "
);
MediaPlayer player;
public
MediaBar(MediaPlayer play)
{
player = play;
setAlignment(Pos.CENTER);
setPadding(
new
Insets(
5
,
10
,
5
,
10
));
vol.setPrefWidth(
70
);
vol.setMinWidth(
30
);
vol.setValue(
100
);
HBox.setHgrow(time, Priority.ALWAYS);
PlayButton.setPrefWidth(
30
);
getChildren().add(PlayButton);
getChildren().add(time);
getChildren().add(volume);
getChildren().add(vol);
PlayButton.setOnAction(
new
EventHandler<ActionEvent>() {
public
void
handle(ActionEvent e)
{
Status status = player.getStatus();
if
(status == status.PLAYING) {
if
(player.getCurrentTime().greaterThanOrEqualTo(player.getTotalDuration())) {
player.seek(player.getStartTime());
player.play();
}
else
{
player.pause();
PlayButton.setText(
">"
);
}
}
if
(status == Status.HALTED || status == Status.STOPPED || status == Status.PAUSED) {
player.play();
PlayButton.setText(
"||"
);
}
}
});
player.currentTimeProperty().addListener(
new
InvalidationListener() {
public
void
invalidated(Observable ov)
{
updatesValues();
}
});
time.valueProperty().addListener(
new
InvalidationListener() {
public
void
invalidated(Observable ov)
{
if
(time.isPressed()) {
player.seek(player.getMedia().getDuration().multiply(time.getValue() /
100
));
}
}
});
vol.valueProperty().addListener(
new
InvalidationListener() {
public
void
invalidated(Observable ov)
{
if
(vol.isPressed()) {
player.setVolume(vol.getValue() /
100
);
}
}
});
}
protected
void
updatesValues()
{
Platform.runLater(
new
Runnable() {
public
void
run()
{
time.setValue(player.getCurrentTime().toMillis()
player.getTotalDuration()
.toMillis()
*
100
);
}
});
}
}