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

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

问题描述

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

public class Main extends Application {公共静态无效主(字符串 [] args){发射(参数);}@覆盖public void start(Stage primaryStage) 抛出异常 {primaryStage.setTitle("媒体");组根 = 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);场景场景 = SceneBuilder.create().width(500).height(500).root(root).fill(Color.WHITE).build();primaryStage.setScene(场景);primaryStage.show();}}

窗口打开但视频不播放,也不例外.有什么问题,我该如何解决.

谢谢.

解决方案

2021 年 10 月更新

我使用 JavaFX 17 再次尝试了这个解决方案,它对我来说效果很好,六年后感觉很好.

请注意,原始示例中的视频不再托管在 YouTube 上,但您仍然希望播放不同的视频.

为此,只需将 url utUPth77L_o 的视频 id 部分替换为您视频的 id(您可以在 YouTube 中播放时在 url 中看到).

在 2021 年有效的示例网址:

观看视频会显示完整的 YouTube 网站、嵌入的视频以及视频的 YouTube 播放器.

YouTube 确实会强制某些视频显示在其网站上,而不是使用嵌入链接,因此即使可以通过观看 URL 在 YouTube 上观看视频,嵌入链接也不会总是有效,但观看链接应该对于这种情况,在 WebView 中工作正常.

例如

2015 年 12 月 4 日更新

某些版本的 JavaFX 8 无法播放 YouTube 视频内容.例如,目前 Java 8u66 无法播放 YouTube 视频内容,但 Java 8u72 抢先体验版可以.

背景

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

解决方案

JavaFX 可以使用 YouTube 视频 URL 播放 YouTube 视频,前提是您将 URL 提供给 WebView 而不是 媒体播放器.

注意事项

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

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

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

示例应用

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

import javafx.application.Application;导入 javafx.scene.Scene;导入 javafx.scene.web.WebView;导入 javafx.stage.Stage;公共类 VideoPlayer 扩展应用程序 {公共静态无效主(字符串 [] args){ 启动(参数);}@Override public void start(Stage stage) 抛出异常 {WebView webview = new WebView();webview.getEngine().load(http://www.youtube.com/embed/utUPth77L_o?autoplay=1");webview.setPrefSize(640, 390);stage.setScene(新场景(webview));舞台表演();}}

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.

解决方案

Update Oct 2021

I tried this solution again using JavaFX 17, it worked fine for me, which is kind of nice six years later.

Note that the video in the original example is no longer hosted on YouTube, but you would want to play a different video anyway.

To do so, just substitute the video id part of the url utUPth77L_o with the id of your video (which you can see in the url when you play it in YouTube).

Example URLs which worked in 2021:

The watch videos display the full YouTube site, the embed videos, just the YouTube player for the video.

YouTube does force some videos to be displayed on its site rather than using the embed link, so even if the video can be watched on YouTube via the watch URL, the embed link won't always work, but the watch link should work OK in WebView for such cases.

For example

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.

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天全站免登陆