使用 JavaFX 播放 Youtube 视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18758041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:48:26  来源:igfitidea点击:

Play a Youtube video using JavaFX

javajavafx

提问by xxlali

I'm trying to play a video from youtube using javaFX. Here is my code

我正在尝试使用 javaFX 播放来自 youtube 的视频。这是我的代码

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.

谢谢。

采纳答案by jewelsea

Update Dec 4th 2015

2015 年 12 月 4 日更新

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.

某些版本的 JavaFX 8 无法播放 YouTube 视频内容。例如,目前 Java 8u66 无法播放 YouTube 视频内容,但 Java 8u72 早期访问版本可以。

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.

关于在 JavaFX 中播放视频的一般信息位于我对以下问题的回答中:用于嵌入电影的任何简单(和最新)Java 框架。这个答案只涉及嵌入 YouTube 视频,因为这似乎是提问者感兴趣的内容。

Solution

解决方案

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

如果您将 URL 提供给WebView而不是MediaPlayer,JavaFX 可以使用 YouTube 视频 URL 播放 YouTube 视频。

Considerations

注意事项

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

如果您只想要 YouTube 媒体播放器而不是整个相关的 YouTube 页面,请使用/embed位置而不是/watchURL 中的位置。

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).

只能嵌入部分视频。例如,您不能嵌入 Katy Perry 视频,因为 YouTube 阻止它以嵌入格式分发(而是告诉您在 YouTube 网站上查看视频,该网站仅通过 YouTube Flash 播放器提供)。

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.

只有 YouTube 允许在其 HTML5 播放器中播放的视频才能在 JavaFX 中播放。这是 YouTube 视频的很大一部分。只能在 YouTube 的 Flash 播放器中播放的 YouTube 视频不能在 JavaFX 中播放。

Sample Application

示例应用程序

The JavaFX application below plays a YouTube video advertisement for a piece of fruit.

下面的 JavaFX 应用程序播放一块水果的 YouTube 视频广告。

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();
  }    
}

回答by agonist_

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.flvyour code works fine

JavaFX 无法仅使用视频 url 播放 youtube 视频。您需要指定视频的文件,而不仅仅是随机的 youtube 链接。尝试使用此 URL:http: //download.oracle.com/otndocs/products/javafx/oow2010-2.flv您的代码工作正常