我正在尝试使用Intellij中的javafx创建一个简单的视频播放器。在此程序中,屏幕的最小高度和宽度是根据运行时的视频设置的,但是当我最大化屏幕时,视频会向左对齐。我希望我的视频在最大化屏幕的中心对齐。我尝试了mediaPlayer.setAlignment()但它没有工作。我想将视频与按钮对齐到中心。
这是我的代码。
public class MoviePlayer extends Application {
public static void main(String[] args) {
launch(args);
}
MediaPlayer mediaPlayer;
private Label time;
Duration duration;
Button fullScreenButton;
Scene scene;
Media media;
double width;
double height;
MediaView mediaView;
//setScene Function
public Scene setScene(double width, double height, Stage primaryStage) {
Group root = new Group();
this.height = height;
this.width = width;
//Add your own path of the vidio that you want to play
String path = "C:/Users/MANJEET SINGH/IdeaProjects/MoviePlayer/Trailers/A.mp4";
media = new Media(new File(path).toURI().toString());
mediaPlayer = new MediaPlayer(media);
//AutoPlay set to false
//mediaPlayer.setAutoPlay(false);
mediaView = new MediaView(mediaPlayer);
final Timeline Slideout = new Timeline();
final Timeline Slidein = new Timeline();
// DropShadow effect
//DropShadow dropshadow = new DropShadow();
//dropshadow.setOffsetY(5.0);
//dropshadow.setOffsetX(5.0);
//dropshadow.setColor(Color.WHITE);
//mediaView.setEffect(dropshadow);
root.setOnMouseExited(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
Slideout.play();
}
});
root.setOnMouseEntered(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
Slidein.play();
}
});
final VBox vbox = new VBox();
Slider slider = new Slider();
vbox.getChildren().add(slider);
//Play/pause Button
Image playButtonImage = new Image("file:///C:/Users/MANJEET SINGH/IdeaProjects/MoviePlayer/buttonImages/play.png");
ImageView iv1 = new ImageView(playButtonImage);
iv1.setFitHeight(25);
iv1.setFitWidth(25);
Image pauseButtonImage = new Image("file:///C:/Users/MANJEET SINGH/IdeaProjects/MoviePlayer/buttonImages/pause.png");
ImageView iv2 = new ImageView(pauseButtonImage);
iv2.setFitHeight(25);
iv2.setFitWidth(25);
final Button playButton = new Button();
playButton.setGraphic(iv1);
playButton.setStyle("-fx-background-color: Black");
//playButton.setAlignment(Pos.BOTTOM_CENTER);
playButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
MediaPlayer.Status status = mediaPlayer.getStatus();
if (status == MediaPlayer.Status.PAUSED
|| status == MediaPlayer.Status.READY
|| status == MediaPlayer.Status.STOPPED) {
mediaPlayer.play();
playButton.setGraphic(iv1);
} else {
mediaPlayer.pause();
playButton.setGraphic(iv2);
}
}
});
playButton.addEventHandler(MouseEvent.MOUSE_ENTERED, (MouseEvent e) -> {
playButton.setStyle("-fx-background-color: Black");
playButton.setStyle("-fx-body-color: Black");
});
playButton.addEventHandler(MouseEvent.MOUSE_EXITED, (MouseEvent e) -> {
playButton.setStyle("-fx-background-color: Black");
});
//Forward Button
Image forwardButtonImage = new Image("file:///C:/Users/MANJEET SINGH/IdeaProjects/MoviePlayer/buttonImages/forwardbutton.png");
ImageView ivforward = new ImageView(forwardButtonImage);
ivforward.setFitHeight(25);
ivforward.setFitWidth(25);
final Button forwardButton = new Button();
forwardButton.setGraphic(ivforward);
forwardButton.setStyle("-fx-background-color: Black");
forwardButton.setOnAction((ActionEvent e) -> {
mediaPlayer.seek(mediaPlayer.getCurrentTime().multiply(1.5));
});
forwardButton.addEventHandler(MouseEvent.MOUSE_ENTERED, (MouseEvent e) -> {
forwardButton.setStyle("-fx-background-color: Black");
forwardButton.setStyle("-fx-body-color: Black");
});
forwardButton.addEventHandler(MouseEvent.MOUSE_EXITED, (MouseEvent e) -> {
forwardButton.setStyle("-fx-background-color: Black");
});
//Backward Button
Image backwardButtonImage = new Image("file:///C:/Users/MANJEET SINGH/IdeaProjects/MoviePlayer/buttonImages/backwardbutton.png");
ImageView ivbackward = new ImageView(backwardButtonImage);
ivbackward.setFitHeight(25);
ivbackward.setFitWidth(25);
final Button backwardButton = new Button();
backwardButton.setGraphic(ivbackward);
backwardButton.setStyle("-fx-background-color: Black");
backwardButton.setOnAction((ActionEvent e) -> {
mediaPlayer.seek(mediaPlayer.getCurrentTime().multiply(0.5));
});
backwardButton.addEventHandler(MouseEvent.MOUSE_ENTERED, (MouseEvent e) -> {
backwardButton.setStyle("-fx-background-color: Black");
backwardButton.setStyle("-fx-body-color: Black");
});
forwardButton.addEventHandler(MouseEvent.MOUSE_EXITED, (MouseEvent e) -> {
backwardButton.setStyle("-fx-background-color: Black");
});
//Reload Button
final HBox hbox2 = new HBox();
final HBox hbox = new HBox();
final int bands = mediaPlayer.getAudioSpectrumNumBands();
final Rectangle[] rects = new Rectangle[bands];
for (int i=0;i<rects.length;i++){
rects[i] = new Rectangle();
rects[i].setFill(Color.GREENYELLOW);
hbox.getChildren().add(rects[i]);
}
vbox.setSpacing(10);
hbox.setPadding(new Insets(2, 20, 0, 0));
hbox2.setAlignment(Pos.CENTER);
hbox2.getChildren().addAll(backwardButton,playButton,forwardButton);
vbox.getChildren().add(hbox2);
vbox.getChildren().add(hbox);
root.getChildren().add(mediaView);
root.getChildren().add(vbox);
Scene scene = new Scene(root, 400, 400, Color.BLACK);
mediaPlayer.setOnReady(new Runnable() {
// run comment
@Override
public void run() {
int w = mediaPlayer.getMedia().getWidth();
int h = mediaPlayer.getMedia().getHeight();
hbox.setMinWidth(w);
int bandwith = w/rects.length;
for (Rectangle r:rects){
r.setWidth(bandwith);
r.setHeight(2);
}
primaryStage.setMinWidth(w);
primaryStage.setMinHeight(h);
vbox.setMinSize(w, 100);
vbox.setMaxSize(scene.getWidth(), scene.getHeight());
vbox.setTranslateY(h-50);
slider.setMin(0.0);
slider.setValue(0.0);
slider.setMax(mediaPlayer.getTotalDuration().toSeconds());
Slideout.getKeyFrames().addAll(
new KeyFrame(new Duration(0),
new KeyValue(vbox.translateYProperty(),h-100),
new KeyValue(vbox.opacityProperty(), 0.9)
),
new KeyFrame(new Duration(300),
new KeyValue(vbox.translateYProperty(),h),
new KeyValue(vbox.opacityProperty(), 0.0)
)
);
Slidein.getKeyFrames().addAll(
new KeyFrame(new Duration(0),
new KeyValue(vbox.translateYProperty(),h),
new KeyValue(vbox.opacityProperty(), 0.0)
),
new KeyFrame(new Duration(300),
new KeyValue(vbox.translateYProperty(),h-100),
new KeyValue(vbox.opacityProperty(), 0.9)
)
);
}
});
//Controllers
mediaPlayer.currentTimeProperty().addListener(new ChangeListener<Duration>() {
@Override
public void changed(ObservableValue<? extends Duration> observable, Duration duration, Duration current) {
slider.setValue(current.toSeconds());
}
});
//Slider Click Property
slider.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
mediaPlayer.seek(Duration.seconds(slider.getValue()));
}
});
//Spectrum Listener
mediaPlayer.setAudioSpectrumListener(new AudioSpectrumListener() {
@Override
public void spectrumDataUpdate(double timestamp, double duration, float[] magnitudes, float[] phases) {
for (int i=0; i < rects.length; i++) {
double h = magnitudes[i] + 60 ;
if(h>2){
rects[i].setHeight(h);
}
}
}
});
return scene;
}
@Override
public void start(Stage primaryStage) throws Exception {
scene = setScene(this.width, this.height,primaryStage);
primaryStage.setTitle("Media Player!");
primaryStage.setScene(scene);
primaryStage.show();
//mediaPlayer.play();
}
}
答案 0 :(得分:0)
StackPane
为我工作。代码中的评论。
import java.io.File;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.media.AudioSpectrumListener;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.media.Media;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class MoviePlayer extends Application {
public static void main(String[] args) {
launch(args);
}
MediaPlayer mediaPlayer;
private Label time;
Duration duration;
Button fullScreenButton;
Scene scene;
Media media;
double width;
double height;
MediaView mediaView;
//setScene Function
public Scene setScene(double width, double height, Stage primaryStage) {
StackPane root = new StackPane();//Change root to StackPane
this.height = height;
this.width = width;
//Add your own path of the vidio that you want to play
String path = "C:\\yourPath\\video.mp4";
media = new Media(new File(path).toURI().toString());
mediaPlayer = new MediaPlayer(media);
//AutoPlay set to false
//mediaPlayer.setAutoPlay(false);
mediaView = new MediaView(mediaPlayer);
final Timeline Slideout = new Timeline();
final Timeline Slidein = new Timeline();
// DropShadow effect
//DropShadow dropshadow = new DropShadow();
//dropshadow.setOffsetY(5.0);
//dropshadow.setOffsetX(5.0);
//dropshadow.setColor(Color.WHITE);
//mediaView.setEffect(dropshadow);
root.setOnMouseExited(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
Slideout.play();
}
});
root.setOnMouseEntered(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
Slidein.play();
}
});
final VBox vbox = new VBox();
Slider slider = new Slider();
vbox.getChildren().add(slider);
//Play/pause Button
Image playButtonImage = new Image("file:///C:/Users/MANJEET SINGH/IdeaProjects/MoviePlayer/buttonImages/play.png");
ImageView iv1 = new ImageView(playButtonImage);
iv1.setFitHeight(25);
iv1.setFitWidth(25);
Image pauseButtonImage = new Image("file:///C:/Users/MANJEET SINGH/IdeaProjects/MoviePlayer/buttonImages/pause.png");
ImageView iv2 = new ImageView(pauseButtonImage);
iv2.setFitHeight(25);
iv2.setFitWidth(25);
final Button playButton = new Button();
playButton.setGraphic(iv1);
playButton.setStyle("-fx-background-color: Black");
//playButton.setAlignment(Pos.BOTTOM_CENTER);
playButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
MediaPlayer.Status status = mediaPlayer.getStatus();
if (status == MediaPlayer.Status.PAUSED
|| status == MediaPlayer.Status.READY
|| status == MediaPlayer.Status.STOPPED) {
mediaPlayer.play();
playButton.setGraphic(iv1);
} else {
mediaPlayer.pause();
playButton.setGraphic(iv2);
}
}
});
playButton.addEventHandler(MouseEvent.MOUSE_ENTERED, (MouseEvent e) -> {
playButton.setStyle("-fx-background-color: Black");
playButton.setStyle("-fx-body-color: Black");
});
playButton.addEventHandler(MouseEvent.MOUSE_EXITED, (MouseEvent e) -> {
playButton.setStyle("-fx-background-color: Black");
});
//Forward Button
Image forwardButtonImage = new Image("file:///C:/Users/MANJEET SINGH/IdeaProjects/MoviePlayer/buttonImages/forwardbutton.png");
ImageView ivforward = new ImageView(forwardButtonImage);
ivforward.setFitHeight(25);
ivforward.setFitWidth(25);
final Button forwardButton = new Button();
forwardButton.setGraphic(ivforward);
forwardButton.setStyle("-fx-background-color: Black");
forwardButton.setOnAction((ActionEvent e) -> {
mediaPlayer.seek(mediaPlayer.getCurrentTime().multiply(1.5));
});
forwardButton.addEventHandler(MouseEvent.MOUSE_ENTERED, (MouseEvent e) -> {
forwardButton.setStyle("-fx-background-color: Black");
forwardButton.setStyle("-fx-body-color: Black");
});
forwardButton.addEventHandler(MouseEvent.MOUSE_EXITED, (MouseEvent e) -> {
forwardButton.setStyle("-fx-background-color: Black");
});
//Backward Button
Image backwardButtonImage = new Image("file:///C:/Users/MANJEET SINGH/IdeaProjects/MoviePlayer/buttonImages/backwardbutton.png");
ImageView ivbackward = new ImageView(backwardButtonImage);
ivbackward.setFitHeight(25);
ivbackward.setFitWidth(25);
final Button backwardButton = new Button();
backwardButton.setGraphic(ivbackward);
backwardButton.setStyle("-fx-background-color: Black");
backwardButton.setOnAction((ActionEvent e) -> {
mediaPlayer.seek(mediaPlayer.getCurrentTime().multiply(0.5));
});
backwardButton.addEventHandler(MouseEvent.MOUSE_ENTERED, (MouseEvent e) -> {
backwardButton.setStyle("-fx-background-color: Black");
backwardButton.setStyle("-fx-body-color: Black");
});
forwardButton.addEventHandler(MouseEvent.MOUSE_EXITED, (MouseEvent e) -> {
backwardButton.setStyle("-fx-background-color: Black");
});
//Reload Button
final HBox hbox2 = new HBox();
final HBox hbox = new HBox();
final int bands = mediaPlayer.getAudioSpectrumNumBands();
final Rectangle[] rects = new Rectangle[bands];
for (int i=0;i<rects.length;i++){
rects[i] = new Rectangle();
rects[i].setFill(Color.GREENYELLOW);
hbox.getChildren().add(rects[i]);
}
vbox.setSpacing(10);
hbox.setPadding(new Insets(2, 20, 0, 0));
hbox2.setAlignment(Pos.CENTER);
hbox2.getChildren().addAll(backwardButton,playButton,forwardButton);
vbox.getChildren().add(hbox2);
vbox.getChildren().add(hbox);
StackPane stackPane = new StackPane();//added StackPane
stackPane.getChildren().add(mediaView);//add MediaView to StackPane root.setBottom(vbox);
root.getChildren().add(stackPane);//add StackPane to root(VBox)
root.getChildren().add(vbox);
Scene scene = new Scene(root, 400, 400, Color.BLACK);
mediaPlayer.setOnReady(new Runnable() {
// run comment
@Override
public void run() {
int w = mediaPlayer.getMedia().getWidth();
int h = mediaPlayer.getMedia().getHeight();
hbox.setMinWidth(w);
int bandwith = w/rects.length;
for (Rectangle r:rects){
r.setWidth(bandwith);
r.setHeight(2);
}
primaryStage.setMinWidth(w);
primaryStage.setMinHeight(h);
vbox.setMinSize(w, 100);
vbox.setMaxSize(scene.getWidth(), scene.getHeight());
vbox.setTranslateY(h-50);
slider.setMin(0.0);
slider.setValue(0.0);
slider.setMax(mediaPlayer.getTotalDuration().toSeconds());
Slideout.getKeyFrames().addAll(
new KeyFrame(new Duration(0),
new KeyValue(vbox.translateYProperty(),h-100),
new KeyValue(vbox.opacityProperty(), 0.9)
),
new KeyFrame(new Duration(300),
new KeyValue(vbox.translateYProperty(),h),
new KeyValue(vbox.opacityProperty(), 0.0)
)
);
Slidein.getKeyFrames().addAll(
new KeyFrame(new Duration(0),
new KeyValue(vbox.translateYProperty(),h),
new KeyValue(vbox.opacityProperty(), 0.0)
),
new KeyFrame(new Duration(300),
new KeyValue(vbox.translateYProperty(),h-100),
new KeyValue(vbox.opacityProperty(), 0.9)
)
);
}
});
//Controllers
mediaPlayer.currentTimeProperty().addListener(new ChangeListener<Duration>() {
@Override
public void changed(ObservableValue<? extends Duration> observable, Duration duration, Duration current) {
slider.setValue(current.toSeconds());
}
});
//Slider Click Property
slider.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
mediaPlayer.seek(Duration.seconds(slider.getValue()));
}
});
//Spectrum Listener
mediaPlayer.setAudioSpectrumListener(new AudioSpectrumListener() {
@Override
public void spectrumDataUpdate(double timestamp, double duration, float[] magnitudes, float[] phases) {
for (int i=0; i < rects.length; i++) {
double h = magnitudes[i] + 60 ;
if(h>2){
rects[i].setHeight(h);
}
}
}
});
return scene;
}
@Override
public void start(Stage primaryStage) throws Exception {
scene = setScene(this.width, this.height,primaryStage);
primaryStage.setTitle("Media Player!");
primaryStage.setScene(scene);
primaryStage.show();
//mediaPlayer.play();
}
}