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
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.
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
The following libraries are necessary to launch an example above:
you can get all of them from a DJ Native Swing download package.
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 */ }
}
}