我有一个用Javafx FXML在Java中创建的简单媒体播放器。
现在我想要实现的,就是在第二个窗口播放视频,遗憾的是我不知道这是怎么做到的。我搜索了有关同一问题的其他帖子,但没有找到任何可以帮助我的内容。
这是代码
simplemediaPlayer.java:
package simplemediaplayer;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
public class simplemediaPlayer extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setTitle("Media Player");
scene.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent doubleClicked) {
if(doubleClicked.getClickCount() == 2) {
if(stage.isFullScreen()) {
stage.setFullScreen(false);
} else {
stage.setFullScreen(true);
}
}
}
});
stage.setScene(scene);
stage.show();
Parent roota = FXMLLoader.load(getClass().getResource("dualmodeWindow.fxml"));
Stage stagea = new Stage();
Scene scenea = new Scene(roota);
stagea.setTitle("Second Window");
stagea.setScene(scenea);
stagea.show();
}
public static void main(String[] args) {
launch(args);
}
}
FXMLDocumentController.java:
package simplemediaplayer;
import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.property.DoubleProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Slider;
import javafx.scene.input.MouseEvent;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.FileChooser;
import javafx.util.Duration;
public class FXMLDocumentController implements Initializable {
private MediaPlayer mediaPlayer;
@FXML
private MediaView mediaView;
private String filePath;
@FXML
private Slider volSlider;
@FXML
private Slider minSlider;
@FXML
private void openvidFile(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Select a File (*.mp4)", "*.mp4");
fileChooser.getExtensionFilters().add(filter);
File file = fileChooser.showOpenDialog(null);
filePath = file.toURI().toString();
if(filePath != null) {
Media media = new Media(filePath);
mediaPlayer = new MediaPlayer(media);
mediaView.setMediaPlayer(mediaPlayer);
DoubleProperty width = mediaView.fitWidthProperty();
DoubleProperty height = mediaView.fitHeightProperty();
width.bind(Bindings.selectDouble(mediaView.sceneProperty(), "width"));
height.bind(Bindings.selectDouble(mediaView.sceneProperty(), "height"));
volSlider.setValue(mediaPlayer.getVolume() * 100);
volSlider.valueProperty().addListener(new InvalidationListener() {
@Override
public void invalidated(Observable observable) {
mediaPlayer.setVolume(volSlider.getValue()/100);
}
});
mediaPlayer.currentTimeProperty().addListener(new ChangeListener<Duration>() {
@Override
public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) {
minSlider.setValue(newValue.toSeconds());
}
});
minSlider.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
mediaPlayer.seek(Duration.seconds(minSlider.getValue()));
}
});
mediaPlayer.play();
}
}
@FXML
private void playVid(ActionEvent event) {
mediaPlayer.play();
mediaPlayer.setRate(1);
}
@FXML
private void pauseVid(ActionEvent event) {
mediaPlayer.pause();
}
@FXML
private void stopVid(ActionEvent event) {
mediaPlayer.stop();
}
@FXML
private void exitProg(ActionEvent event) {
System.exit(0);
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
}
FXMLDocument.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.media.MediaView?>
<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" stylesheets="@style.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="simplemediaplayer.FXMLDocumentController">
<children>
<MediaView fx:id="mediaView" fitHeight="200.0" fitWidth="200.0" />
<BorderPane prefHeight="200.0" prefWidth="200.0">
<top>
<MenuBar fx:id="topMenu" BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" onAction="#openvidFile" text="Open" />
<MenuItem mnemonicParsing="false" onAction="#exitProg" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
<bottom>
<VBox fx:id="botMenu" alignment="BOTTOM_CENTER" prefHeight="84.0" prefWidth="600.0" BorderPane.alignment="CENTER">
<children>
<Slider fx:id="minSlider" />
<HBox alignment="CENTER" prefHeight="57.0" prefWidth="600.0">
<children>
<Button fx:id="openClick" mnemonicParsing="false" onAction="#openvidFile" text="Open">
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</Button>
<Button fx:id="playClick" mnemonicParsing="false" onAction="#playVid" text="Play">
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</Button>
<Button fx:id="pauseClick" mnemonicParsing="false" onAction="#pauseVid" text="Pause">
<HBox.margin>
<Insets right="20.0" />
</HBox.margin>
</Button>
<Slider fx:id="volSlider" prefHeight="25.0" prefWidth="140.0" />
<Button fx:id="dualmodeButton" mnemonicParsing="false" text="Dual Mode">
<HBox.margin>
<Insets left="30.0" />
</HBox.margin>
</Button>
</children>
</HBox>
</children>
</VBox>
</bottom>
</BorderPane>
</children>
</StackPane>
我创建了第二个fxml窗口,它只包含一个堆栈窗口和一个mediaview。
dualmodeWindow.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.media.MediaView?>
<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="simplemediaplayer.FXMLDocumentController">
<children>
<MediaView fx:id="secmediaView" fitHeight="200.0" fitWidth="200.0" />
</children>
</StackPane>
我试图在第二个窗口上为mediaView提供与主窗口上的mediaView相同的ID,但第二个窗口上没有显示视频。
如果有人可以帮助我,那会很好。
答案 0 :(得分:0)
您需要创建具有相同媒体对象的第二个媒体播放器,然后将此第二个媒体播放器设置为第二个媒体视图。根据文档,媒体播放器只能有一个媒体对象。