I tried to play a video in a javafx app; this is my code:
public class Video extends Application{
private String Dir = System.getProperty("user.dir") + "\\out\\video";
public static void main(String[] args) throws Exception{
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
File f = new File(Dir, "test.mp4");
Media media = new Media(f.toURI().toURL().toString());
MediaPlayer player = new javafx.scene.media.MediaPlayer(media);
MediaView viewer = new MediaView(player);
player.setOnReady(() -> {
player.setStartTime(Duration.ZERO);
player.setStopTime(media.getDuration().subtract(Duration.valueOf("50")));
});
player.setOnEndOfMedia(() -> player.stop());
StackPane root = new StackPane();
root.getChildren().add(viewer);
Scene scenes = new Scene(root, 500, 500, Color.BLACK);
stage.setScene(scenes);
stage.setTitle("Riddle Game");
stage.setFullScreen(true);
stage.show();
player.play();
}
}
But when I launch the app, it shows a black window and the video is not played.
I don't understand my mistake
Thanks in advance for your help.
EDIT : Still don't work...
Related
3d software allow user to change draw mode dinamically. It can be implemented on javafx ?
Changing draw mode with radio buttons
In this approach a Box instance change its DrawMode with radiobuttons.
This is a single class javafx you can try .
App.java
public class App extends Application {
#Override
public void start(Stage stage) {
var perspective = new PerspectiveCamera(true);
perspective.setNearClip(0.1);
perspective.setFarClip(500);
perspective.setTranslateZ(-150);
Shape3D cube = new Box(50, 50, 50);
cube.setCullFace(CullFace.NONE);
cube.setMaterial(new PhongMaterial(Color.CORAL));
var toggleGroup = new ToggleGroup();
var solid = new RadioButton("solid");
solid.setToggleGroup(toggleGroup);
solid.setSelected(true);
var wire = new RadioButton("wireframe");
wire.setToggleGroup(toggleGroup);
var hBox = new HBox(solid, wire);
toggleGroup.selectedToggleProperty().addListener((o) -> {
Toggle selectedToggle = toggleGroup.getSelectedToggle();
if (selectedToggle == solid) {
cube.setDrawMode(DrawMode.FILL);
}
if (selectedToggle == wire) {
cube.setDrawMode(DrawMode.LINE);
}
});
var group3d = new Group(perspective, cube);
var subscene = new SubScene(group3d, 300, 400, true, SceneAntialiasing.BALANCED);
subscene.setCamera(perspective);
var stack = new StackPane(subscene, hBox);
stage.setScene(new Scene(stack, 300, 400));
stage.show();
}
public static void main(String[] args) {
launch();
}
}
Please, consider the following code:
public class JavaFxTest4 extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
Button button = new Button();
button.setText("dialog");
button.setOnAction((e) -> {
Dialog<?> d = new Dialog<>();
final Window window = d.getDialogPane().getScene().getWindow();
Stage stage = (Stage) window;
stage.setMinHeight(450);
stage.setMaxHeight(450);
stage.setHeight(450);
stage.setMinWidth(600);
stage.setMaxWidth(600);
stage.setWidth(600);
window.setY(300); //<---- note this line
window.setX(660); //<---- note this line
d.showAndWait();
});
VBox root = new VBox();
root.getChildren().addAll(button);
var scene = new Scene(root, 1920, 1000);
primaryStage.setScene(scene);
primaryStage.show();
}
}
As you can see window position is 660 (x) and 300 (y). And this is the result:
As you can see x position is correct, but y position is not. Is this a bug or I misunderstand something? I use javafx 19-ea+3 and openjdk version "14.0.2" on Ubuntu 20.
It is a bug in JavaFX. Issue is here
Today I faced the following problem:
I set title for primaryStage using method setTitle(). This title is displayed correctly in the window, but when I hover cursor on program icon in OS dockbar I see just path to main program class (see screenshot below). How I can fix it?
Title in program window and title in OS dockbar. And I use this code:
#Override
public void start(Stage stage) throws IOException {
this.stage = stage;
instance = this;
// App initialization
Parent root = FXMLLoader.load(SlimeBot.class.getClassLoader().getResource("fxml/Main.fxml"));
Scene scene = new Scene(root, 800, 600);
stage.setTitle("SlimeBot - Панель управления");
stage.setScene(scene);
stage.show();
}
Trying set title using field in java.awt.Toolkit:
#Getter
private static SlimeBot instance;
static {
try {
Toolkit xToolkit = Toolkit.getDefaultToolkit();
java.lang.reflect.Field awtAppClassNameField = xToolkit.getClass().getDeclaredField("awtAppClassName");
awtAppClassNameField.setAccessible(true);
awtAppClassNameField.set(xToolkit, "SlimeBot - Панель управления");
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
launch(args);
}
#Getter
private Stage stage;
#Override
public void start(Stage stage) throws IOException {
this.stage = stage;
instance = this;
// App initialization
Parent root = FXMLLoader.load(SlimeBot.class.getClassLoader().getResource("fxml/Main.fxml"));
Scene scene = new Scene(root, 800, 600);
stage.setTitle("SlimeBot - Панель управления");
stage.setScene(scene);
stage.show();
}
}```
I want to do a simple JavaFX application to watch Twitch Live Streams.
So far, I come up with 3 possible solutions:
1) JavaFX WebView. With it I was successful to watch Twitch clip video, but Live Steaming seems not supported.
Code:
public class App extends Application {
public static void main(String[] args) {
launch();
}
#Override
public void start(Stage stage) {
WebView webview = new WebView();
webview.getEngine().load(
"https://www.twitch.tv/stariy_bog/clip/RelievedPopularYakinikuDancingBanana"
);
WebView webView = new WebView();
webview.setPrefSize(640, 390);
stage.setScene(new Scene(webview));
stage.show();
}
}
2) JavaFX scene.media package. I’ve found this package in JavaFX documentation: it states that HLS Live Streaming is supported, but I’ve found no success in loading any Twitch channel.
Code:
public class MediaTest extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
String uri = "https://www.twitch.tv/qsnake";
try {
primaryStage.setTitle("Embedded Media Player");
Group root = new Group();
Scene scene = new Scene(root, 540, 210);
Media media = new Media(uri);
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
// create mediaView and add media player to the viewer
MediaView mediaView = new MediaView(mediaPlayer);
((Group) scene.getRoot()).getChildren().add(mediaView);
primaryStage.setScene(scene);
primaryStage.sizeToScene();
primaryStage.show();
mediaPlayer.setOnPlaying(() -> {
System.out.println(mediaPlayer.getMedia().getDuration().toString());
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
3) Twitch Developers API. Did not found any information about live steams using Java here so far.
Also, yes, I’ve searched web about this problem. For example I’ve found a topic, but it OPs problem was not resolved.
I am new to Java and JavaFX all together, so please be patient with me if I am asking the wrong question here.
I am trying to add preloader Scene to an application I built: I was able to add the preloader using the code below. It's the default preloader and it looks very basic. So, here is my question. 1) how to add percentage progress status instead of progressbar. 2) Is it possible to load this progress bar on top of an FXML scene
Preloader
public class JavaFXPreloader3 extends Preloader {
ProgressBar bar;
Stage stage;
private Scene createPreloaderScene() throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("FXML.fxml"));
ProgressIndicator pi = new ProgressIndicator();
BorderPane p = new BorderPane();
p.setCenter(pi);
return new Scene(p, 300, 150);
}
#Override
public void start(Stage stage) throws Exception {
this.stage = stage;
stage.setScene(createPreloaderScene());
stage.show();
}
#Override
public void handleStateChangeNotification(StateChangeNotification scn) {
if (scn.getType() == StateChangeNotification.Type.BEFORE_START) {
stage.hide();
}
}
#Override
public void handleProgressNotification(ProgressNotification pn) {
bar.setProgress(pn.getProgress());
}
}
How do I return root scene instead of the BorderPane p including the Progress Indicator:
ProgressIndicator pi = new ProgressIndicator();
BorderPane p = new BorderPane();
p.setCenter(pi);
return new Scene(p, 300, 150);
This is a excellent tutorial for creating a PreLoader.
https://docs.oracle.com/javafx/2/deployment/preloaders.htm