javajavafxwebviewvlcj

Upcalls from JavaScript to JavaFX not working due to VLCJ


Upcalls from JavaScript to JavaFX works fine. But if I add a variable containing the VLCJ player implementation then everything stops working.

Main.java:

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javafx.concurrent.Worker;
import javafx.concurrent.Worker.State;
import netscape.javascript.JSObject;

public class Main extends Application {
    WebView browser = new WebView();
    WebEngine webEngine = browser.getEngine();
    VLC player = new VLC(); // It doesn't work because of it. But if you remove that, then everything works.

    @Override
    public void start(final Stage stage) {
        webEngine.setJavaScriptEnabled(true);
        String HTML_STRING = //
                "<html>"//
                        + "<head> " //
                        + "  <script language='javascript'> " //
                        + "     function callToJavaFX()  { "//
                        + "        myJavaMember.log('text'); " //
                        + "     } " //
                        + "  </script> "//
                        + "</head> "//
                        + "<body> "//
                        + "   <h2>This is Html content</h2> "//
                        + "   <button onclick='callToJavaFX();'>Call To JavaFX</button> "//
                        + "</body> "//
                        + "</html> "//
                ;
        webEngine.loadContent(HTML_STRING);
        Worker<Void> worker = webEngine.getLoadWorker();
        worker.stateProperty().addListener(new ChangeListener<State>() {
            @Override
            public void changed(ObservableValue<? extends State> observable, State oldValue, State newValue) {
                if (newValue == Worker.State.SUCCEEDED) {
                    JSObject jsobj = (JSObject) webEngine.executeScript("window");
                    jsobj.setMember("myJavaMember", new JSBridge());
                }
            }
        });

        StackPane stack_pane = new StackPane(browser);
        stage.setScene(new Scene(stack_pane,700, 400, Color.BLACK));
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

JSBridge.java:

public class JSBridge {
    public void log(String text) {
        System.out.println(text);
    }
}

VLC.java:

import uk.co.caprica.vlcj.factory.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;

public class VLC{

    private final MediaPlayerFactory mediaPlayerFactory;
    private final EmbeddedMediaPlayer mediaPlayer;

    public VLC() {
        this.mediaPlayerFactory = new MediaPlayerFactory();
        this.mediaPlayer = mediaPlayerFactory.mediaPlayers().newEmbeddedMediaPlayer(); // It doesn't work because of it. But if you remove that, then everything works.
    }
}

My modules: enter image description here

So far, I have only been able to figure out that upcalls is not working due to the declaration of the variable 'player' in Main.java. But it will work if you change this.mediaPlayer = mediaPlayerFactory.mediaPlayers (). NewEmbeddedMediaPlayer () in VLC.java; on this.mediaPlayer = null

If you remove the line 'VLC player = new VLC ();' then everything works. If it is not deleted, then the method 'myJavaMember.log (' text ')' will not work and there will be an error: 'TypeError: myJavaMember.log is not a function (In' myJavaMember.log ('text') ',' myJavaMember.log ' is undefined). Why is that?

Please tell me how to fix this and what could be the reason?

P.S.: I know how to implement VLCJ. It works for me. Here is only the code that can repeat the error that worries me. The problem is that upcalls doesn't work. I repeat how to implement the VLCJ player I know.


Solution

  • Solved the problem thanks to this answer: https://stackoverflow.com/a/53618875/10946427

    It turns out my JSBridge was being destroyed by the garbage collector and because of VLCJ it was faster.

    If you create a JSBridge variable inside the class, then the garbage collector will not destroy it and everything will work.