0

I'm trying to get play a youtube livestream using JavaFX 8.

package main;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.web.WebView;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
    try {
        WebView embeddedWV = new WebView();
        embeddedWV.getEngine().loadContent(
                "<iframe width=\"1280\" height=\"720\" src=\"https://www.youtube.com/embed/5qap5aO4i9A\" "
                + "frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\""
                + " allowfullscreen></iframe>","text/html");
            embeddedWV.setPrefSize(640, 400);
            root.getChildren().add(embeddedWV);

            primaryStage.setScene(scene);
            primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

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

}

The embedded stream loads fine but when I click play, youtube tells me that my "browser" doesn't support any of the video formats. I tried with webview and with mediaplayer, but I can't even get the page to load in mediaplayer.

Maschs
  • 35
  • 8
  • `MediaPlayer` can't load Youtube videos. It can only load normal videos in a file system. – Pulse Sep 04 '20 at 20:05

1 Answers1

0

As stated in comments you could try to update JavaFX to a more recent release If it still doesn't work then you could try some alternatives like JxBrowser or JCEF both based on Chromium however the first one is very expensive and the latter is a bit difficult to implement in JavaFX

  • Isn't complaining about no updates and still using Java 8 a contradiction in itself? In a couple of days we will be officially at JavaFX 15. – mipa Sep 07 '20 at 07:03
  • Have a look at this older question: https://stackoverflow.com/questions/61460446/unable-to-display-video-in-webpage-using-javafx-webview-video-is-live-on-the-gi – mipa Sep 07 '20 at 07:09
  • @mipa I said that because I recently tried to use it with Google Oauth2 and it wasn't working very well. Then I had a look on GitHub and in jfx14 branch the latest commit on WebEngine was made two years ago. They did some updates on jfx15 though. Personally I see JavaFX's WebEngine as a toy, it doesn't compare to chromium based browsers. But you are right I will edit my answer – Alessandro Parisi Sep 07 '20 at 08:54