2013-07-24

Java Gangnam Player

I modified the previous code to playback a YouTube video using the webview within Java FX.



JavaVideoPlayer2.java
1 package JPRG;
 2 
 3 import javafx.application.Platform;
 4 import javafx.embed.swing.JFXPanel;
 5 import javafx.scene.web.*;
 6 import javafx.scene.Scene;
 7 import javafx.scene.layout.StackPane;
 8 import java.awt.*;
 9 import javax.swing.*;
10 
11 public class JavaVideoPlayer2 extends JFrame {
12 
13     private static final int JFXPANEL_WIDTH_INT = 640;
14     private static final int JFXPANEL_HEIGHT_INT = 360;
15     private static JFXPanel fxContainer;
16     private String htmlcontent = 
17         "<iframe width=\"640\" height=\"360\" " + 
18         "src=\"http://www.youtube.com/embed/9bZkp7q19f0?rel=0\" " + 
19         "frameborder=\"0\" allowfullscreen></iframe>";
20     
21     
22     public static void main(String[] args) {
23         SwingUtilities.invokeLater(new Runnable() {
24             @Override
25             public void run() {
26                 JavaVideoPlayer2 frame = new JavaVideoPlayer2();
27                 frame.setSize(640,480);
28                 frame.initialize();
29                 frame.setDefaultCloseOperation(
30                         JFrame.EXIT_ON_CLOSE);
31                 frame.pack();
32                 frame.setLocationRelativeTo(null);
33                 frame.setVisible(true);
34             }
35         });
36     }
37 
38     public void initialize() {
39         fxContainer = new JFXPanel();
40         fxContainer.setPreferredSize(
41                 new Dimension(JFXPANEL_WIDTH_INT, 
42                 JFXPANEL_HEIGHT_INT));
43         add(fxContainer, BorderLayout.CENTER);
44         // create JavaFX scene
45         Platform.runLater(new Runnable() {
46             @Override
47             public void run() {
48                 createScene();
49             }
50         });
51     }
52 
53     private void createScene() {
54         WebView webView = new WebView();
55         WebEngine webEngine = webView.getEngine();
56         webEngine.loadContent(htmlcontent);
57 
58         StackPane root = new StackPane();
59         root.getChildren().add(webView);
60         fxContainer.setScene(new Scene(root));
61     }
62 }
63 

No comments:

Post a Comment

Github CoPilot Alternatives (VSCode extensions)

https://www.tabnine.com/blog/github-copilot-alternatives/