我想使用这个类来仅使用构造函数运行媒体文件,而不使用main方法。 (我从GUI运行这个Player类)如何在没有main方法的情况下使用luanch()?
public class Player extends Application {
File file = null; // a file to play.
public Player (File file){
this.file = file;
}
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws MalformedURLException {
URL url = file.getAbsoluteFile().toURI().toURL(); // URL path of the file.
final Media m = new Media(url.toString());
final MediaPlayer mp = new MediaPlayer(m);
final MediaView mv = new MediaView(mp);
final DoubleProperty width = mv.fitWidthProperty();
final DoubleProperty height = mv.fitHeightProperty();
width.bind(Bindings.selectDouble(mv.sceneProperty(), "width"));
height.bind(Bindings.selectDouble(mv.sceneProperty(), "height"));
mv.setPreserveRatio(true);
StackPane root = new StackPane();
root.getChildren().add(mv);
final Scene scene = new Scene(root, 960, 540);
scene.setFill(Color.BLACK);
primaryStage.setScene(scene);
primaryStage.show();
mp.play();
}
}
答案 0 :(得分:1)
您可以通过调用扩展Application的类的静态方法来实现。所以调用Player.launch(Player.class)
应该这样做。另请注意,不得多次调用它或抛出异常。
答案 1 :(得分:1)
Application.launch
是启动应用程序的方法。您只能启动一个应用程序。使用此方法启动的任何应用程序都需要提供非arg构造函数,该构造函数在应用程序生命周期中用于构造应用程序类。您不能使用自己构建的应用程序类的实例。
您可以将String
作为参数传递,然后使用getParameters
方法访问这些参数:
public static void startPlayer(File file) throws MalformedURLException {
Application.launch(Player.class, file.toURI().toURL().toString());
}
Player
class private String url; // a file url to play.
public Player() {
}
@Override
public void init() throws Exception {
url = getParameters().getUnnamed().get(0);
// TODO: handle invalid parameters
}
@Override
public void start(Stage primaryStage) throws MalformedURLException {
final Media m = new Media(url);
...
创建一个非应用程序Player
类,并从应用程序类中使用它。这允许您在应用程序中创建多个播放器窗口,并且或多或少独立于应用程序生命周期:
public class AlternativePlayer {
public AlternativePlayer(Stage primaryStage, File file) throws MalformedURLException {
URL url = file.getAbsoluteFile().toURI().toURL(); // URL path of the file.
final Media m = new Media(url.toString());
mp = new MediaPlayer(m);
final MediaView mv = new MediaView(mp);
final DoubleProperty width = mv.fitWidthProperty();
final DoubleProperty height = mv.fitHeightProperty();
width.bind(Bindings.selectDouble(mv.sceneProperty(), "width"));
height.bind(Bindings.selectDouble(mv.sceneProperty(), "height"));
mv.setPreserveRatio(true);
StackPane root = new StackPane();
root.getChildren().add(mv);
final Scene scene = new Scene(root, 960, 540);
scene.setFill(Color.BLACK);
primaryStage.setScene(scene);
}
private final MediaPlayer mp;
public void play() {
mp.play();
}
}
使用这种方法,您还需要一个正在运行的应用程序,因此它可能是这样的:
public class PlayerMain extends Application {
@Override
public void start(Stage primaryStage) throws MalformedURLException {
AlternativePlayer player = new AlternativePlayer(primaryStage, new File("myvideofile"));
primaryStage.show();
player.play();
}
public static void main(String[] args) {
launch(args);
}
}
答案 2 :(得分:0)
不确定它是否完全正确但是如何调用提供该文件的此Player类的单例?
某些代码可能如下(在您的Player类中):
private static Player instance;
public static Player getInstance(File file) {
if (instance == null) {
instance = new Player(file);
}
return instance;
}
private Player (File file){
this.file = file;
}
然后通过调用
在另一个类中使用它Player.getInstance(new File("test.file")).launch(Player.class);