spring boot javafx load video from resource folder - javafx

Hi i have a spring boot javafx app. I want to load video from resources/video/a.mp4 .
final Media m = new Media(getClass().getResource("/video/a.mp4").toString());
this.player = new MediaPlayer(m);
final MediaView view = new MediaView(player);
final DoubleProperty width = view.fitWidthProperty();
final DoubleProperty height = view.fitHeightProperty();
width.bind(Bindings.selectDouble(view.sceneProperty(), "width"));
height.bind(Bindings.selectDouble(view.sceneProperty(), "height"));
view.setPreserveRatio(true);
mainlayout.getChildren().add(view);
player.play();
The code above not playing video. Always showing nullpointer exception.

Related

javafx vlcj video freezes after few seconds while audio keeps playing

I am making a javafx media player using vlcj.
There is my DMediaPlayer.java class that creates the vlcj media player and returns an ImageView which displays the video.
and here is my PlayerView.java
this class displays the video.
public class PlayerView extends BorderPane {
private MenuBar menuBar;
private VideoControlPanel controlPanel;
private Drawer drawer;
private IconsProvider icons;
private MediaPlayerInterface mediaPlayer;
private ImageView mediaView;
private StackPane playerHolder;
private ImageView view;
public PlayerView() {
view = new ImageView();
icons = new IconsProvider();
menuBar = new MenuBar(icons);
setTop(menuBar);
playerHolder = new StackPane();
playerHolder.setAlignment(Pos.TOP_LEFT);
setCenter(playerHolder);
mediaPlayer = new DMediaPlayer();
mediaView = mediaPlayer.getMediaView();
playerHolder.getChildren().add(mediaView);
try {
mediaPlayer.load("/home/doruk/Downloads/Video/movie.mkv");
} catch (FileNotFoundException e) {
System.out.println(e.toString());
}
mediaPlayer.play();
playerHolder.getChildren().add(drawer);
}
}
The problem is the video plays as expected, but after few seconds, the ImageView only displays the single image, its like the video is paused. but the audio keeps on playing.
I thought the issue was due to ImageView being garbage collected as if I initially when the mediaplayer was called as follow:
DMediaPlayer player = new DMediaPlayer()
player.play()
inside the PlayerView class, there would be following errors:
JNA: callback object has been garbage collected
JNA: callback object has been garbage collected
JNA: callback object has been garbage collected
malloc(): unaligned tcache chunk detected
and so I created the the ImageView in the same PlayerView class and passed the reference to the DMediaPlayer so that it can play the video in it, but that does not work either.

Play music in JavaFX

I have the following code to play music but cannot play the whole track. Music plays for 3 seconds then stops.
(I'm new to javaFX and hope anyone can help)
void Music(ActionEvent event) throws Exception {
String path = "Music.mp3";
playHitSound(path);
}
private void playHitSound(String fileName) {
String path = getClass().getResource(fileName).getPath();
Media media = new Media(new File(path).toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);
mediaPlayer.play();
}

open javafx window with it's relative path

I need to open a fxml window from a javafx controler
Stage graphStage = new Stage();
FXMLLoader loader = new FXMLLoader();
fenetre = new File("../graph/graph.fxml");
if (!fenetre.exists()) {
System.out.println("fichier inexistant");
} else {
Pane rootGraph = loader.load(getClass().getResource(fenetre.getPath()).openStream());
GraphController controller = (GraphController) loader.getController();
controller.getSystemChoice(id);
Scene sceneGraph = new Scene(rootGraph);
sceneGraph.getStylesheets().add(getClass().getResource("myStyle.css").toExternalForm());
graphStage.setTitle("Graphe");
graphStage.setScene(sceneGraph);
graphStage.show();
}
the code doesn't fincd the file, how to reach this file ?
controler is on: cloud/composant/controler.java
my fxml is on: cloud/graph/graph.fxml
Either you get the data from a file or you use a resource.
Do not try to combine both approaches. They are often incompatible.
// assuming here the file path is correct
FXMLLoader loader = new FXMLLoader(fenetre.toURI().toURL());
...
Pane rootGraph = loader.load();
If the file is a resource, you should prefer to load it as resource though:
// assuming cloud is positioned in the "classpath root"
FXMLLoader loader = new FXMLLoader(getClass().getResource("/cloud/graph/graph.fxml"));

How to add sound to the pop-up window in javafx [duplicate]

This question already has answers here:
Java FX 2 Alert sound
(4 answers)
Closed 6 years ago.
How can I add a sound notification when a window pops up? I want to add my custom sound file, not the windows sound.
Code:
public class PopupWindow {
Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
public void display(String newCase) throws IOException {
Stage window = new Stage();
Image applicationIcon = new Image(getClass().getResourceAsStream("../images/logo.jpg"));
window.getIcons().add(applicationIcon);
window.initStyle(StageStyle.UNDECORATED);
Label txtNotification = new Label("Новых заявок:");
Label notification = new Label(newCase);
notification.setTranslateX(120);
notification.setStyle("-fx-text-fill: crimson");
Button closeButton = new Button("x");
closeButton.setTranslateX(205);
closeButton.setTranslateY(5);
closeButton.setOnAction(event -> window.close());
Pane layout = new Pane();
layout.getChildren().addAll(txtNotification, notification, closeButton);
window.setX(primaryScreenBounds.getMinX() + primaryScreenBounds.getWidth() - 245);
window.setY(primaryScreenBounds.getMinY() + primaryScreenBounds.getHeight() / 1.2);
Scene scene = new Scene(layout);
window.setScene(scene);
layout.getStylesheets().add(this.getClass().getResource("../style/popup.css").toExternalForm());
window.setAlwaysOnTop(true);
window.showAndWait();
}
}
Play an AudioClip, just before you call showAndWait() on your pop-up window:
AudioClip plonkSound = new AudioClip("http://somehost/path/plonk.aiff");
plonkSound.play();
If you want to load the audio clip from your class path (e.g. from your jar file in a sibling location to your PopopWindow.class), then get the clip as a resource:
AudioClip plonkSound = new AudioClip(
PopupWindow.getResource("plonk.aiff").toExternalForm()
);

How to make the JavaFX ScrollPane fill the SWT composite all the time, even the SWT Form is resized

I have a JavaFX ScrollPane embedded inside a SWT composite, and the composite is filled in side a SWT Form. How to make the JavaFX ScrollPane fill the SWT composite all the time, even the SWT Form is resized.
Here is a example how I use a FXCanvas to hold the JavaFX content:
public Control createContents(Composite parent) {
final Composite composite = (Composite) super.createContents(parent);
composite.setLayout(new FillLayout());
// Create a JavaFX canvas
final FXCanvas fxCanvas = new FXCanvas(composite, SWT.NONE);
/* Create a JavaFX Group node */
Group group = new Group();
start(group);
fxCanvas.setScene(scene);
return composite;
}
public void start(final Group stage) {
Parent zoomPane = createZoomPane(group);
stage.getChildren().add(zoomPane);
}
private Parent createZoomPane(final Group group) {
final double SCALE_DELTA = 1.1;
final StackPane zoomPane = new StackPane();
zoomPane.getChildren().add(group);
scroller = new ScrollPane();
final Group scrollContent = new Group(zoomPane);
scroller.setContent(scrollContent);
return scroller;
}
After my experiment, I found the answer by myself, it's a common issue for JavaFx actually, not related with SWT.
The reason is I created a Group as the root and add it to the Scene, I should just create a VBox as the root and add it to the Scene.

Resources