将 YouTube 视频嵌入到 JFrame?

2022-01-19 00:00:00 youtube-api java

我一直在做大量的研究并试图找到一个指南来教我如何正确地将 YouTube 视频直接嵌入到我的 JFrame 中.我已阅读 YouTube API 上的所有 Google Developers 指南,但找不到我想要做的事情.

I've been doing a lot of research and trying to find a guide that can teach me how to correctly embed a YouTube video directly to my JFrame. I've read all of the Google Developers guides on the YouTube API but can't find just what I'm looking to do.

我正在尝试使用我的 main 方法中的 init 将 YouTube 视频直接嵌入到 JFrame 中.例如:

I'm trying to embed a YouTube video straight to the JFrame using an init in my main method. For example:

/**
 * Main
 * @param args
 * @throws IOException
 */
public static void main(String[] args) throws IOException {
    try {
        UIManager.setLookAndFeel(new NimbusLookAndFeel());
    } catch (UnsupportedLookAndFeelException ulafe) {
        Loader loader = new Loader();
        loader.doFrame();
    }
    Start Loader = new Start();
    Loader.setVisible(true);
}

/**
 * Start
 * @throws IOException
 */
public Start() throws IOException {
    super("Ryan's Client Launcher version: '0.0.1'");
    try {
        getContentPane().setBackground(Color.BLACK);
        setBackground(Color.BLACK);

        BufferedImage image39 = ImageIO.read(getClass().getResourceAsStream("\jaggl\igs\39.png"));

        this.setIconImage(image39);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(new Dimension(launcherWidth, launcherHeight));
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setUndecorated(true); 
        getContentPane().setLayout(null);

        initWorldSelectInterface();

    } catch (IOException e) {
        System.out.println(e);
    }
}

在我启动我的世界选择界面下,我希望有initYouTubeVideo"

Under where I have initiated my world select interface I would like to have "initYouTubeVideo"

我创建了一个无效的initYouTubeVideo",但我并不完全了解如何嵌入视频和只有视频.假设我为我的游戏和我制作了一个关于它的 YouTube 视频.我想在我的 JFrame 上展示该视频(没有评论部分或其他任何内容,只是简单的 YouTube 视频).

I've created a void "initYouTubeVideo" and don't exactly understand how I can just embed the video & only the video. Say I make an update for my game & I make a YouTube video about it. I'd like to feature that video on my JFrame (no comment Section or anything else just simply the YouTube video).

谁能给我一个指导,我可以直接学习如何嵌入 YouTube 视频.

Could someone please give me a guide that I can learn from directly on how to Embed the YouTube video.

对于这篇文章,我制作了一个示例 JFrame,以帮助您直观地了解我想要完成的工作.这是图片:

For this post I made an example JFrame to help you visually understand what I'm trying to accomplish. Here is the Image:

在图片中,红色是我放置 YouTube 播放器的位置.绿色是我的接口.现在再次说我在我的游戏中进行了更新&制作了一个关于它的 youtube 视频.我希望能够将视频的链接放在 &当有人运行客户端时,他们可以点击播放 &观看视频.

In the image the red is where I would place the YouTube player & The green is my interfaces. Now again say I made an update in my game & made a youtube video about it. I want to be able to just put the link to the video in & when someone runs the client they can click play & watch the video.

注意:我正在使用 Google 的 YouTube API

我查看了以下指南:有关 YouTube API 的所有信息都可以在这里找到:https://developers.google.com/youtube/v3/开始使用

I have looked at the following Guides: Everything about the YouTube API found here: https://developers.google.com/youtube/v3/getting-started

同样,我只想将视频添加到 JFrame,以便我的玩家可以获取有关最新游戏内容的视频更新.

Again All i want is just to add the video to the JFrame so my players can get video updates on the latest game content.

谢谢你&代表那些可以帮助我的人.

Thank you & Rep to those that can help me.

推荐答案

这是我通常用来将 YouTube 视频嵌入 Swing 应用程序的方式.

Here's the way I usualy use to embed YouTube videos into Swing application.

使用 DJ Native Swing 将原生浏览器组件嵌入到 JPanel 中,而不是 YouTube API:p>

Instead of YouTube API a native browser component is embedded into JPanel using DJ Native Swing:

public class YouTubeViewer {

public static void main(String[] args) {
    NativeInterface.open();
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JFrame frame = new JFrame("YouTube Viewer");
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.getContentPane().add(getBrowserPanel(), BorderLayout.CENTER);
            frame.setSize(800, 600);
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    });
    NativeInterface.runEventPump();
    // don't forget to properly close native components
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        @Override
        public void run() {
            NativeInterface.close();
        }
    }));
}

public static JPanel getBrowserPanel() {
    JPanel webBrowserPanel = new JPanel(new BorderLayout());
    JWebBrowser webBrowser = new JWebBrowser();
    webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
    webBrowser.setBarsVisible(false);
    webBrowser.navigate("https://www.youtube.com/v/b-Cr0EWwaTk?fs=1");
    return webBrowserPanel;
}
}

运行时的样子

启动上面的示例需要以下库:

The following libraries are necessary to launch an example above:

  • DJNativeSwing.jar
  • DJNativeSwing-SWT.jar
  • swt-4.3-win32-win32-x86.jar(这个依赖于平台)

您可以从 DJ Native Swing 下载包中获取所有这些.

you can get all of them from a DJ Native Swing download package.

相关文章