Play Youtube Video Using JavaFX

I am trying to play a youtube video using javaFX. Here is my code

public class Main extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) throws Exception { primaryStage.setTitle("Media"); Group root = new Group(); Media media = new Media("http://www.youtube.com/watch?v=k0BWlvnBmIE"); MediaPlayer mediaPlayer = new MediaPlayer(media); mediaPlayer.play(); MediaView mediaView = new MediaView(mediaPlayer); root.getChildren().add(mediaView); Scene scene = SceneBuilder.create().width(500).height(500).root(root) .fill(Color.WHITE).build(); primaryStage.setScene(scene); primaryStage.show(); } } 

A window will open, but the video will not play, and there are no exceptions. What is the problem and how can I fix it.

Thanks.

+6
source share
2 answers

December 4, 2015 Patch

Some versions of JavaFX 8 cannot play YouTube video content. Currently, for example, Java 8u66 cannot play YouTube video content, but there may be a Java 8u72 early access release.

Background

General information about playing video in JavaFX is in my answer to: Any simple (and updated) Java frames for embedding movies . This answer only applies to embedding videos on YouTube, as this seems like what interests the question.

Decision

JavaFX can play YouTube videos using the YouTube URL if you specify the URL of the WebView , not MediaPlayer .

Questions

If you just want to use the YouTube media player and not the entire YouTube page associated with it, use the location /embed , not the location /watch in the URL.

Only some videos are embedded. For example, you cannot embed a Katy Perry video because YouTube blocks its distribution in the embedded format (instead, you are invited to watch the video on YouTube, where it is only available through the YouTube Flash Player).

In JavaFX, only videos that YouTube can play in its HTML5 player can be played. This is a fairly large percentage of YouTube videos. YouTube videos that play only on YouTube Flash Player do not play in JavaFX.

Application example

The JavaFX app below plays YouTube ads for a piece of fruit.

fruit

 import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.web.WebView; import javafx.stage.Stage; public class VideoPlayer extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) throws Exception { WebView webview = new WebView(); webview.getEngine().load( "http://www.youtube.com/embed/utUPth77L_o?autoplay=1" ); webview.setPrefSize(640, 390); stage.setScene(new Scene(webview)); stage.show(); } } 
+11
source

JavaFX cannot play YouTube videos using a movie. you need to specify the file of your video, and not just a random link to YouTube. Try with this url: http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv your code works fine

+2
source

Source: https://habr.com/ru/post/953682/


All Articles