使用JavaFX播放Youtube视频 [英] Play a Youtube video using JavaFX

查看:156
本文介绍了使用JavaFX播放Youtube视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

谢谢。

推荐答案

2015年12月4日更新

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

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中播放视频的一般信息,请参阅我的答案:任何简单(及时更新) )用于嵌入电影的Java框架。这个答案只涉及嵌入YouTube视频,因为这似乎是提问者感兴趣的内容。

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.

解决方案

如果您向 WebView 而不是 MediaPlayer

注意事项

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

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.

只能嵌入一些视频。例如,您无法嵌入Katy Perry视频,因为YouTube会阻止其以嵌入格式发布(而是告诉您在YouTube网站上观看视频,而该视频仅通过YouTube Flash播放器提供)。

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

只有YouTube允许在其HTML5播放器中播放的视频才能在JavaFX中播放。这是YouTube视频的绝大部分比例。仅在YouTube的Flash播放器中播放的YouTube视频无法在JavaFX中播放。

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.

示例应用

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

The JavaFX application below plays a YouTube video advertisement for a piece of 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();
  }    
}

这篇关于使用JavaFX播放Youtube视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆