S_Teo
S_Teo

Reputation: 31

JavaFx webview YouTube

A couple of weeks ago, i used NetBeans to create one of it's javafx examples
The webview project, and it worked fine.
I am especially interested in using javafx to play youtube videos
in a swinginterop application.
This is part of a bigger project, so i used the SwingInterop example from NetBeans
to load the players on a JFrame
I need to load more than one YouTube Video.
And again everything worked great.
I am using Windows XP, NetBeans 7.2, Java 1.7 9
These days i tried the application again but this time on youtube i see
"This Video is currently unavailable".
The first thing that came into my mind was that maybe
i don't have the latest FlashPlayer ...
Needless to say i downloaded everything...
The application just won't play youtube videos anymore
I even reinstalled java and netbeans again but nothing changed.
I also mention that on Firefox, which is the browser i have on my pc
youtube works just fine.
It is pretty annoying to know that ... something was ok and maybe ...
i found a way to mess things in there ...
So right now, i can see web pages in javafx webview but youtube videos are still not playing...

The last time i asked for help i did not put the code ... sorry about that.

So i made another small project with only two classes in it to show what i did

I did not test this on another pc because, if it happened once
chances are it will happen again and since things don't fix themselves
i want to be able to answer this question when it will pop up.

Thank you jewelsea and Gregory for the interest

here is the code :

the frame on which the players are loaded

package stackoverflow;

import java.awt.Dimension;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;

public class PlayersFrame extends JFrame
{
    public PlayersFrame()
    {
        // initialize jframe
        setLayout(null);
        setTitle("   YouTube on JFrame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        CreatePlayersList();
    }

    // all the players have the same size
    Dimension PlayerSize = new Dimension(300, 230);    

    private void CreatePlayersList()
    {
        /*
         * create an instance of playerclass
         * containing the location and the youtube embedded address
         * for each of the four players
         */

        Point NewPlayerLocation = null;
        PlayerClass NewPlayer = null;

        NewPlayerLocation = new Point(10, 10);
        NewPlayer = new PlayerClass(NewPlayerLocation, PlayerSize, "http://www.youtube.com/embed/SvcDwPlaWgw?rel=0");
        // add player1 class to the players list
        Players.add(NewPlayer);

        NewPlayerLocation = new Point(320, 10);
        NewPlayer = new PlayerClass(NewPlayerLocation, PlayerSize, "http://www.youtube.com/embed/L0huXvTeVvU?rel=0");
        // add player2 class to the players list
        Players.add(NewPlayer);

        NewPlayerLocation = new Point(10, 240);
        NewPlayer = new PlayerClass(NewPlayerLocation, PlayerSize, "http://www.youtube.com/embed/rHcnsEoSK_c?rel=0");
        // add player3 class to the players list
        Players.add(NewPlayer);

        NewPlayerLocation = new Point(320, 240);
        NewPlayer = new PlayerClass(NewPlayerLocation, PlayerSize, "http://www.youtube.com/embed/vaXuK-RsT6E?rel=0");
        // add player4 class to the players list
        Players.add(NewPlayer);

        // stand by
        LoadPlayers();
    }

    // list of players data
    List<PlayerClass> Players = new ArrayList<>();

    /*
     * a class to hold data about a player
     */
    public class PlayerClass
    {
        public PlayerClass(Point playerLocation, Dimension playerSize, String youTubeAddress)
        {
            PlayerLocation = playerLocation;
            PlayerSize = playerSize;
            YouTubeAddress = youTubeAddress;
        }
        public Point PlayerLocation = null;
        public Dimension PlayerSize = null;
        public String YouTubeAddress = null;
    }

    /*
     * in this swinginterop project i want to display youtube videos on a jframe
     * with webview i get more that i ask for
     * webview will display an entire web page
     * so a web page is created on the fly containing a frame for the player
     */
    private String GeneratePlayerPage(Dimension PlayerSize, String EmbeddedAddress)
    {
        String GeneratedPage = null;

        GeneratedPage = "<html>\n";        
        GeneratedPage += "<body>\n";        
        GeneratedPage += "<iframe\n";        
        GeneratedPage += "style=\"position:absolute;\n";
        GeneratedPage += "left:0px;top:0px;" + 
                "width:" + String.valueOf(PlayerSize.width) +
                "px;height:" + String.valueOf(PlayerSize.height) + "px;\"\n";
        GeneratedPage += "src=\"" + EmbeddedAddress + "\"" + "\n";
        GeneratedPage += "frameborder=\"0\" allowfullscreen>\n";        
        GeneratedPage += "</iframe>\n";        
        GeneratedPage += "</body>\n";        
        GeneratedPage += "</html>";

        return(GeneratedPage);
    }

    /*
     * why synchronized ? ...
     * 
     * each javafx object runs on it's own thread
     * this means that, sooner or later
     * you will have to deal with crossthreading issues
     * 
     * in this project it does not throw an error
     * but since the playersform will add controls on a locked procedure,
     * and i don't use a single player on the jframe,
     * some of the players will not appear on the frame.
     *
     * if only one player is loaded on the jframe
     * then all this is not necessary.
     *
     * i don't know if the players are there but are not rendered
     * or the players were just not loaded
     * 
     * with this synchronized void a callback from the javafx object is handled
     */
    public synchronized void OkPlayer()
    {
        try
        {
            /*
             * a player was loaded
             * delay before the next player
             */
            Thread.sleep(10);
        }
        catch(Exception Ex)
        {}

        /*
         * if the players list is empty than all the players have been loaded
         * but you also get an error, so check the size ...
         */
        if(Players.size() > 0)
        {
            LoadPlayers();
        }
    }

    void LoadPlayers()
    {
        /*
         * get the data from the first playerclass in the players list
         * and create a new player
         */
        PlayerClass TempPlayer = Players.get(0);
        Dimension playerSize = TempPlayer.PlayerSize;
        YouTubePlayer player = new YouTubePlayer(this, playerSize,
        GeneratePlayerPage(playerSize, TempPlayer.YouTubeAddress));
        add(player);        
        player.setLocation(TempPlayer.PlayerLocation);
        player.setSize(TempPlayer.PlayerSize);
        player.setVisible(true);
        // remove the used playerclass from the players list
        Players.remove(0);
    }
}

and the player

package stackoverflow;

import java.awt.Dimension;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UnsupportedLookAndFeelException;

public final class YouTubePlayer extends JPanel
{
    public YouTubePlayer(PlayersFrame playersFrame, Dimension PlayerSize, String YouTubeAddress)
    {
        remote = playersFrame;
        init(PlayerSize, YouTubeAddress);
    }

    private static JFXPanel browserFxPanel;
    private Pane browser;

    public void init(Dimension playerSize, String youTubeAddress) 
    {
        final Dimension PlayerSize = playerSize;
        final String YouTubeAddress = youTubeAddress;
        browserFxPanel = new JFXPanel();
        browserFxPanel.setPreferredSize(new Dimension(PlayerSize.width, PlayerSize.height));        
        add(browserFxPanel);

        Platform.runLater(new Runnable() 
        {
            public void run() 
            {
                // inside the player's thread
                createScene(PlayerSize, YouTubeAddress);
            }
        });
    }

    /*
     * the program will start on the javafx thread
     * the main void is placed in the javafx component
     * 
     * maybe there are other ways to launch the program
     * but for this one it is good enough ... for now
     */
    public static void main(String[] args) 
    {
        SwingUtilities.invokeLater(new Runnable() 
        {
            public void run() 
            {
                try
                {
                    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) 
                    {
                        // set windows look and feel
                        if ("Windows".equals(info.getName())) 
                        {
                            javax.swing.UIManager.setLookAndFeel(info.getClassName());
                            break;
                        }
                    }
                }
                catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException Ex)//ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) 
                {}

                // create an instance of playersframe
                PlayersFrame LoadPlayers = new PlayersFrame();
                LoadPlayers.setSize(new Dimension(640, 520));
                // make it visible
                LoadPlayers.setVisible(true);        
            }
        });
    }

    PlayersFrame remote = null;

    private void createScene(Dimension PlayerSize, String YouTubeAddress) 
    {
        browser = createBrowser(PlayerSize, YouTubeAddress);
        browserFxPanel.setScene(new Scene(browser));        
        /*
         * player loaded
         * let playersform know that it can proceed
         * with the next player, if there still is one
         */
        remote.OkPlayer();
    }

    private Pane createBrowser(Dimension PlayerSize, String YouTubeAddress) 
    {
        WebView view = new WebView();
        view.setPrefSize(PlayerSize.width, PlayerSize.height);
        final WebEngine eng = view.getEngine();
        eng.loadContent(YouTubeAddress);
        GridPane grid = new GridPane();
        grid.getChildren().addAll(view);
        return grid;
    }
}

this is what it looks like

https://i.sstatic.net/UvhcJ.jpg

and this is the 'thing'

https://i.sstatic.net/lB4uY.jpg

the last player acts the same as the others

If i use the example from NetBeans and just paste a YouTube link
i get the same result

I really need to know what is happening

Thank you

Upvotes: 3

Views: 2113

Answers (1)

AZ_
AZ_

Reputation: 21899

Check out the system requirements:

http://docs.oracle.com/javafx/2/system_requirements_2-2/jfxpub-system_requirements_2-2.htm

For Windows XP and Windows Vista, JavaFX Media 2.2 requires that one of the following external modules be installed to play AAC audio and H.264/AVC video: 
MainConcept H.264/AVC Pro Decoder Pack
DivX Plus Codec Pack
MainConcept Showcase (includes demo version codecs)

DivX is freely downloadable: http://www.divx.com/

Reference: https://forums.oracle.com/thread/2467376

Upvotes: 1

Related Questions