Embed a YouTube video to JFrame?

27,088

Solution 1

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

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

When running it looks like

enter image description here

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)

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

Solution 2

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 */ }
  }
  }

Solution 3

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

    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.

Share:
27,088

Related videos on Youtube

Ryan
Author by

Ryan

Updated on September 26, 2022

Comments

  • Ryan
    Ryan over 1 year

    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.

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

    Under where I have initiated my world select interface I would like to have "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).

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

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

    Image

    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.

    Note: I am using Google's YouTube API

    I have looked at the following Guides: Everything about the YouTube API found here: 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.

    Thank you & Rep to those that can help me.

  • Ryan
    Ryan over 10 years
    :? I've heard about using swing/DJ & I've been told it's not the best way to go -.- that YouTube's API was the best for placing it on a JFrame & with DJ the video doesn;t always load &| never shows up.
  • Ryan
    Ryan over 10 years
    & with this will I be able to control where I put it on my JPanel & it's size or would I have to create an image button that the user has to click & it launches the JPanel w/ the video?
  • Jk1
    Jk1 over 10 years
    1. I had never expirienced such troubles with DJ myself. Maybe such behavior may be observed on certain platforms only, as swt library used is platform-dependent. 2. As you may see from the code sample player is located on an ordinary JPanel. So it behaves like any other JPanel in terms of size and location.
  • Ryan
    Ryan over 10 years
    Yeah I discovered that. & Is there any guide's on advancing the youtube player. I'm sorry if i'm frustrating you, I'm trying to learn. I want to learn not have the code handed to me :) but the code above I made a example project & added the jars to the build path, pasted the code & tried running it & got a huge list of errors. I've pasted the errors here: pastebin.com/KrGBaR1F Am I missing another Jar or?
  • Jk1
    Jk1 over 10 years
    No problem) As I've said before swt labrary is platform dependent. Example above has been created for 32x Windows, while your OS is obviosly 64x. You can find appropriate swt library for any platform at eclipse.org/swt
  • Jk1
    Jk1 over 10 years
    One option is to create a separate build for every platform you're interested in. It's possible, however, to create cross-platform swt application without maintaining many builds, some useful approaches are discussed here: stackoverflow.com/questions/2706222/…
  • Mayukh Nair
    Mayukh Nair over 9 years
    +1 for the best Youtube Embedding method I've ever seen.
  • Mayukh Nair
    Mayukh Nair over 9 years
    Hang on: is this the entire Youtube Webpage for the video or just the wideget stretched to 800x600? I know this post is really old, but I'm really curious.
  • Jeel Shah
    Jeel Shah about 9 years
    Is it possible to reduce the size to say 300 x 400 or something arbitrary like that?
  • Zack Downs
    Zack Downs about 9 years
    @JeelShah Yea I believe you just have to use the setSize method and pass in the 2 integers you want, width first then height.
  • Jeel Shah
    Jeel Shah about 9 years
    so webBrowser.setSize(x,y)?
  • Zack Downs
    Zack Downs almost 9 years
    Yup that looks right. I would double check on Google though, it's been a while since I've done any Java, or work on this subject.
  • benchpresser
    benchpresser over 8 years
    DJBrowser does not work on macosx. (There is no swt support for macosx)
  • gumuruh
    gumuruh about 7 years
    is there any other Oracle's official API that works just like DJ Project did? I'm curious wonder why... @benchpresser

Related