Java 将 YouTube 视频嵌入 JFrame?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20440484/
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
Embed a YouTube video to JFrame?
提问by Ryan
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 APIbut can't find just what I'm looking to do.
我一直在进行大量研究,并试图找到一个指南来教我如何将 YouTube 视频直接正确地嵌入到我的JFrame
. 我已阅读YouTube API上的所有 Google Developers 指南,但找不到我想要做的事情。
I'm trying to embed a YouTube video straight to the JFrame
using an init in my main method. For example:
我试图JFrame
在我的主要方法中使用 init直接嵌入 YouTube 视频。例如:
/**
* 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);
}
}
Under where I have initiated my world select interface I would like to have "initYouTubeVideo
"
在我启动我的世界选择界面的地方,我想要“ initYouTubeVideo
”
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).
我创建了一个空白“ initYouTubeVideo
”并且不完全理解我如何只嵌入视频和视频。假设我更新了我的游戏并制作了一个关于它的 YouTube 视频。我想在我的JFrame
(无评论部分或其他任何内容只是 YouTube 视频)上展示该视频。
Could someone please give me a guide that I can learn from directly on how to Embed the YouTube video.
有人可以给我一个指南,我可以直接从中学习如何嵌入 YouTube 视频。
For this post I made an example JFrame to help you visually understand what I'm trying to accomplish. Here is the Image:
在这篇文章中,我制作了一个 JFrame 示例,以帮助您直观地理解我要完成的任务。这是图像:
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.
在图像中,红色是我放置 YouTube 播放器的位置,绿色是我的界面。现在再次说我在我的游戏中进行了更新并制作了一个关于它的 YouTube 视频。我希望能够将视频链接放入,当有人运行客户端时,他们可以点击播放并观看视频。
Note: I am using Google's YouTube API
注意:我使用的是Google 的 YouTube API
I have looked at the following Guides: Everything about the YouTube API found here: https://developers.google.com/youtube/v3/getting-started
我查看了以下指南:有关 YouTube API 的所有内容都在这里找到:https: //developers.google.com/youtube/v3/getting-started
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.
同样,我想要的只是将视频添加到 JFrame,以便我的玩家可以获得有关最新游戏内容的视频更新。
Thank you & Rep to those that can help me.
感谢您和代表可以帮助我的人。
采纳答案by Jk1
Here's the way I usualy use to embed YouTube videos into Swing application.
这是我通常用来将 YouTube 视频嵌入 Swing 应用程序的方式。
Instead of YouTube API a native browser component is embedded into JPanel using DJ Native Swing:
使用DJ Native Swing将原生浏览器组件嵌入到 JPanel 中,而不是 YouTube API :
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;
}
}
When running it looks like
运行时看起来像
The following libraries are necessary to launch an example above:
启动上面的示例需要以下库:
- DJNativeSwing.jar
- DJNativeSwing-SWT.jar
- swt-4.3-win32-win32-x86.jar (This one is platform dependent)
- DJNativeSwing.jar
- DJNativeSwing-SWT.jar
- swt-4.3-win32-win32-x86.jar (这个是平台相关的)
you can get all of them from a DJ Native Swing download package.
您可以从 DJ Native Swing 下载包中获得所有这些。
回答by Neo
You can try this code: This code will redirect the click to the browser.
你可以试试这个代码:这个代码会将点击重定向到浏览器。
public class YoutubePlay
{
public static void main(String[] args) throws URISyntaxException {
final URI uri = new URI("http://www.youtube.com/watch?v=qzW6mgfY5X4");
class OpenUrlAction implements ActionListener
{
@Override public void actionPerformed(ActionEvent e) {
open(uri);
}
}
JFrame frame = new JFrame("Links");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(100, 400);
Container container = frame.getContentPane();
container.setLayout(new GridBagLayout());
JButton button = new JButton();
button.setText("<HTML>Click the <FONT color=\"#000099\"><U>link</U></FONT>");
button.setHorizontalAlignment(SwingConstants.LEFT);
button.setBorderPainted(false);
button.setOpaque(false);
button.setBackground(Color.WHITE);
button.setToolTipText(uri.toString());
button.addActionListener(new OpenUrlAction());
container.add(button);
frame.setVisible(true);
}
private static void open(URI uri)
{
if (Desktop.isDesktopSupported())
{
try
{
Desktop.getDesktop().browse(uri);
}
catch (IOException e)
{ /* TODO: error handling */ }
}
else
{ /* TODO: error handling */ }
}
}
回答by Zack Downs
I don't have the reputation to comment and explain what's going on Mayukh, but I wanted to help you out. The trick here (In Jk1's answer) is in the
我没有资格评论和解释 Mayukh 的情况,但我想帮助你。这里的技巧(在 Jk1 的回答中)在
webBrowser.navigate("https://www.youtube.com/v/b-Cr0EWwaTk?fs=1");
The original link would be:
原来的链接是:
https://www.youtube.com/watch?v=b-Cr0EWwaTk
but you switch it to:
但是您将其切换为:
https://www.youtube.com/v/b-Cr0EWwaTk?fs=1
to get the video in a "Full Screen-In Browser" view.
在“全屏浏览器”视图中获取视频。