Play a Youtube video using JavaFX


Question

I'm trying to play a video from youtube 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();
}

}

The window opens but video doesn't play and there is no exception. What is the problem and how can i fix it.

Thanks.

1
8
6/25/2017 5:17:34 PM

Accepted Answer

Update Dec 4th 2015

Some versions of JavaFX 8 are unable to play back youtube video content. Currently, for instance, Java 8u66 cannot playback youtube video content, but Java 8u72 early access release can.

Background

General information on playing video in JavaFX is located in my answer to: Any simple (and up to date) Java frameworks for embedding movies. This answer just deals with embedding YouTube videos as that appears to be what the question asker is interested in.

Solution

JavaFX can play a YouTube video using a YouTube video URL if you supply the URL to a WebView rather than a MediaPlayer.

Considerations

If you just want the YouTube media player and not the whole related YouTube page, use the /embed location rather than the /watch location in the URL.

Only some videos can be embedded. For instance, you can't embed the Katy Perry video because YouTube blocks it's distribution in an embedded format (instead telling you to view the video on the YouTube site, where it is only provided through the YouTube Flash player).

Only videos which YouTube allow to play in their HTML5 player may be played in JavaFX. This is a pretty large percentage of YouTube videos. YouTube videos which only play in YouTube's Flash player do not play in JavaFX.

Sample Application

The JavaFX application below plays a YouTube video advertisement 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();
  }    
}
15
5/23/2017 12:10:39 PM

JavaFX can't play youtube video just with the video url. you need to specify the file of your video, not just a random youtube link. Try with this URL : http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv your code works fine


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon