Play Youtube Video in JavaFX - javafx

Is there is any way to play Youtube videos on JavaFX Application? I was trying this-
public class YoutubeVideoPlayer extends Application
{
#Override
public void start(Stage stage) throws Exception
{
WebView webview = new WebView();
webview.getEngine().load("http://www.youtube.com/embed/_3op5hukpIE?autoplay=1");
webview.setPrefSize(640, 390);
stage.setScene(new Scene(webview));
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
I don't know how, but once it worked fine. But every time it is showing me an error message:
An error occured. Please try again later.
Can anyone explain how did it work and how can I make it work again?

This code is not working when I was running this on IDE(Eclipse, NetBeans or IntelliJ). But when I am Exporting this from Eclipse IDE as "Runnable JAR file", and running the Jar file, it's working perfectly. It seems, it's just not running on IDE.

Related

In JavaFX using JxBrowser invokeAndWaitFinishLoadingMainFrame() method crashes JVM

I'm experimenting with the JXBrowser Chromium browser engine in JavaFX on Mac OS Sierra. I would like to wait until the URL is fully loaded after I call browser.goBack() or browser.goForward() methods so I can check the Navigation History. The simple app below crashes the JVM but the same code works fine in Java (Swing). The same call in a Java swing app works without any issues. Does anyone have any idea why?
public class JavaFXSample extends Application {
#Override
public void init() throws Exception {
// On Mac OS X Chromium engine must be initialized in non-UI thread.
if (Environment.isMac()) {
BrowserCore.initialize();
}
}
#Override
public void start(final Stage primaryStage) {
Browser browser = new Browser();
BrowserView view = new BrowserView(browser);
Scene scene = new Scene(new BorderPane(view), 700, 500);
primaryStage.setScene(scene);
primaryStage.show();
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent t) {
Platform.exit();
System.exit(0);
}
});
Browser.invokeAndWaitFinishLoadingMainFrame(browser, new Callback<Browser>
() {
#Override
public void invoke(Browser browser) {
browser.loadURL("http://www.google.com");
}
});
}
public static void main(String[] args) {
launch(args);
}
}
Looks like you've faced a deadlock because you create the Browser instance in heavyweight mode. You can try solving this issue by using the "jxbrowser.ipc.external=true" VM parameter that enables lightweight rendering mode and runs Chromium engine in separate native process to avoid deadlocks in UI thread.

JavaFX MediaPlayer - seek() method makes the player hang

I've been looking for ages for direction on this matter and I finally post here.
I have a JavaFX application with MediaPlayer. One day, seeking at a later position in video (that had not been accessed previously) started hanging the player. No status change before it gets to PLAYING, the buffer is loaded, status at READY before I call seek().
First I thought it is because I went out of Application thread, tried to put the MediaPlayer back on the root to be sure, and the seek method worked as before, fast enough for me.
But then for a reason I can't get, it started hanging again all the time, with same symptoms.
Now, even with the most simple code, it hangs too.
I'm desperate, the waiting time can be 30 seconds to reach a position 2 minutes later in the video. Looks like the Media Player is scanning again all video until it finds the good position it's seeking, thus taking more time for a later position. If the position has been accessed before though, seek() won't hang...
Am I the only one with this problem?
I'm on Mac os EL Capitan, but tried on Windows VM too and I get the same behaviour.
Here is a standalone code, but I don't see how it will help, I don't even hope for ppl to reproduce:
public class VideoPlayerExample extends Application {
public static void main(String[] args) throws Exception {
launch(args);
}
#Override
public void start(final Stage stage) throws Exception {
File file = new FileChooser().showOpenDialog(stage);
Media media = new Media(file.toURI().toString());
MediaPlayer mp = new MediaPlayer(media);
mp.statusProperty().addListener(new ChangeListener<MediaPlayer.Status>() {
#Override
public void changed(ObservableValue<? extends Status> observable, Status oldValue, Status newValue) {
System.out.println(newValue);
}
});
Group gp = new Group(new MediaView(mp));
Button buttonTest = new Button("It's gonna hang...");
buttonTest.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
mp.pause();
System.out.println(mp.getCurrentTime().toMillis());
mp.seek(new Duration(mp.getCurrentTime().toMillis() +10000));
mp.play();
}
});
gp.getChildren().add(buttonTest);
stage.setScene(new Scene(gp, 540, 208));
stage.show();
}
}
Any help will be so greatly appreciated!
You're right - I can't reproduce your problem. I have macOS Sierra 10.12.6. All I can say is check the type of movie you're trying to play - not all encodings are supported. Also, according to the documentation, if the movie's duration is Duration.INDEFINITE, seek() will have no effect.
Place the seek method in a new thread not on your JavaFX thread.
new Thread(() -> mp.seek(new Duration(mp.getCurrentTime().toMillis() +10000)))).start();

javafx programmatically set arguments for virtual keyboard

I have a desktop application that will be used on computers with no keyboard, input will be on a touch screen. I can get the virtual keyboard to show up on textfields fine when running from eclipse. I used these arguments
-Dcom.sun.javafx.touch=true
-Dcom.sun.javafx.isEmbedded=true
-Dcom.sun.javafx.virtualKeyboard=none
The following link shows me where to add the arguments.
how to add command line parameters when running java code in Eclipse?
When I make a runnable jar file the keyboard does not show up. I am looking for a way to set these arguments programmatically so that the runnable jar file will display the virtual keyboard on any computer. Any help will be greatly appreciated.
The only working solution I could find came from here
Gradle build for javafx application: Virtual Keyboard is not working due to missing System property
Create a wrapper class and set the system properties before invoking the applications original main method.
public class MainWrapper {
public static void main(String[] args) throws Exception
{ // application - package name
Class<?> app = Class.forName("application.Main");
Method main = app.getDeclaredMethod("main", String[].class);
System.setProperty("com.sun.javafx.isEmbedded", "true");
System.setProperty("com.sun.javafx.touch", "true");
System.setProperty("com.sun.javafx.virtualKeyboard", "javafx");
Object[] arguments = new Object[]{args};
main.invoke(null, arguments);
}
}
When making the runnable jar file just point to the MainWrapper class for the launch configuration.
The -D option to the JVM sets a system property. So you can achieve the same by doing the following:
public class MyApplication extends Application {
#Override
public void init() {
System.setProperty("com.sun.javafx.touch", "true");
System.setProperty("com.sun.javafx.isEmbedded", "true");
System.setProperty("com.sun.javafx.virtualKeyboard", "none");
}
#Override
public void start(Stage primaryStage) {
// ...
}
}

JavaFX UI issue(css and colors are not appearing) in windows 10 virtual box

Can any one help me on the below issue..
I am working on JavaFX and i have developed an standalone application using javafx. when i run the application in windows 10 it is working and the same is not working on windows 10 virtual box.
i have developed this application using JDK8u60 and created an executable jar file.when i run the jar file i am getting below issue.
Issue : colors of javafx ui is not appearing properly/UI shaded on virtual box. i have commented css and checked but still facing the same issue.
can any one please let me know a proper solution or root cause for this issue and let me know if any input required.
public class My_UI extends Application {
public static My_Controller controller;
private Stage stage;
private BorderPane root;
#Override
public void start(final Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(My_UI.class.getResource("My_GUI.fxml"));
root = (BorderPane) loader.load();
controller = (My_Controller)loader.getController();
Platform.setImplicitExit(false);
stage.setMaximized(true);
stage.setScene(new Scene(root));
stage.initStyle(StageStyle.UNIFIED);
stage.show();
}
}

JavaFX UI hangs

I am writing a JavaFX program under ecilpse, It works well on my local machine i.e., I can execute the runable jar after I export. However, when I put the executable jar to another machine, the UI was not responding. Here are the codes I launch the javaFX program.
#Override
public void start(Stage primaryStage) {
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent e) {
Platform.exit();
System.exit(0);
}
});
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Server Simulator");
context = new ClassPathXmlApplicationContext("classpath:PrTrSim.xml");
this.displayQueue = (LinkedBlockingQueue<Message>) context.getBean("displayQueue");
this.userInputQueue = (LinkedBlockingQueue<Message>) context.getBean("userInputQueue");
this.outgoingQueue = (LinkedBlockingQueue<Message>) context.getBean("outgoingQueue");
this.incomingQueue = (LinkedBlockingQueue<Message>) context.getBean("incomingQueue");
addQueue.add(this.displayQueue);
addQueue.add(this.outgoingQueue);
addQueue.add(this.incomingQueue);
initRootLayout();
showSimOverview();
}
public static void main(String[] args) {
launch(args);
}
The PrTrSim.xml is for initialization of two components(messageProcessor and SocketIO reader) which are running behind.The 4 blocking queues are for message receiving and handling.
To avoid blocking of the main thread you should use the JavaFX concept of tasks or services as explained in details here: http://docs.oracle.com/javafx/2/threads/jfxpub-threads.htm

Resources