Embed a YouTube video to JFrame?

前端 未结 3 1654
礼貌的吻别
礼貌的吻别 2020-12-03 09:09

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

相关标签:
3条回答
  • 2020-12-03 09:23

    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.

    0 讨论(0)
  • 2020-12-03 09:38

    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.

    0 讨论(0)
  • 2020-12-03 09:44

    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 */ }
      }
      }
    
    0 讨论(0)
提交回复
热议问题