【发布时间】:2012-11-02 22:44:26
【问题描述】:
几周前,我使用 NetBeans 创建了其中一个 javafx 示例
webview 项目,它运行良好。
我对使用 javafx 播放 youtube 视频特别感兴趣
在 swinginterop 应用程序中。
这是一个更大项目的一部分,所以我使用 NetBeans 中的 SwingInterop 示例
在 JFrame 上加载播放器
我需要加载多个 YouTube 视频。
一切都很好。
我正在使用 Windows XP、NetBeans 7.2、Java 1.7 9
这些天我再次尝试了该应用程序,但这次我在 youtube 上看到了
“此视频目前不可用”。
我首先想到的是也许
我没有最新的 FlashPlayer ...
不用说我下载了所有东西...
该应用程序将不再播放 youtube 视频
我什至再次重新安装了 java 和 netbeans,但没有任何改变。
我还提到了 Firefox,这是我电脑上的浏览器
youtube 工作得很好。
知道这很烦人......有些事情还可以,也许......
我找到了一种把东西弄乱的方法...
所以现在,我可以在 javafx webview 中看到网页,但 youtube 视频仍然没有播放...
上次我寻求帮助时,我没有输入代码……对此感到抱歉。
所以我又做了一个小项目,里面只有两个类来展示我做了什么
我没有在另一台电脑上测试这个,因为如果它发生过一次
它很可能会再次发生,因为事情不会自行解决
我希望能够在它弹出时回答这个问题。
感谢 Jewelsea 和 Gregory 的关注
这里是代码:
加载播放器的框架
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);
}
}
和播放器
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;
}
}
这就是它的样子
http://i.stack.imgur.com/UvhcJ.jpg
这就是“东西”
http://i.stack.imgur.com/lB4uY.jpg
最后一位玩家的行为与其他玩家相同
如果我使用 NetBeans 中的示例并粘贴 YouTube 链接
我得到相同的结果
我真的需要知道发生了什么
谢谢
【问题讨论】:
-
请添加指向不可用视频的链接。
-
了解 Java/JavaFX 的操作系统和版本也会有所帮助。您的操作系统最近更新了吗?
-
嗨 Gregory 我在 windows xp 上,我有 java 1.7_9 和 netbeans 7.2
-
我已经从 oracle 女巫下载了最新的 sdk,其中包含 javafx
-
当我在netbeans中编译javafx程序时,它说它检测到javafx ant api 1.2,我知道ant是用来部署javafx应用的
标签: javafx