Watch Twitch Live Stream in JavaFX - java

I want to do a simple JavaFX application to watch Twitch Live Streams.
So far, I come up with 3 possible solutions:
1) JavaFX WebView. With it I was successful to watch Twitch clip video, but Live Steaming seems not supported.
Code:
public class App extends Application {
public static void main(String[] args) {
launch();
}
#Override
public void start(Stage stage) {
WebView webview = new WebView();
webview.getEngine().load(
"https://www.twitch.tv/stariy_bog/clip/RelievedPopularYakinikuDancingBanana"
);
WebView webView = new WebView();
webview.setPrefSize(640, 390);
stage.setScene(new Scene(webview));
stage.show();
}
}
2) JavaFX scene.media package. I’ve found this package in JavaFX documentation: it states that HLS Live Streaming is supported, but I’ve found no success in loading any Twitch channel.
Code:
public class MediaTest extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
String uri = "https://www.twitch.tv/qsnake";
try {
primaryStage.setTitle("Embedded Media Player");
Group root = new Group();
Scene scene = new Scene(root, 540, 210);
Media media = new Media(uri);
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
// create mediaView and add media player to the viewer
MediaView mediaView = new MediaView(mediaPlayer);
((Group) scene.getRoot()).getChildren().add(mediaView);
primaryStage.setScene(scene);
primaryStage.sizeToScene();
primaryStage.show();
mediaPlayer.setOnPlaying(() -> {
System.out.println(mediaPlayer.getMedia().getDuration().toString());
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
3) Twitch Developers API. Did not found any information about live steams using Java here so far.
Also, yes, I’ve searched web about this problem. For example I’ve found a topic, but it OPs problem was not resolved.

Related

javafx doesn't launch the video

I tried to play a video in a javafx app; this is my code:
public class Video extends Application{
private String Dir = System.getProperty("user.dir") + "\\out\\video";
public static void main(String[] args) throws Exception{
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
File f = new File(Dir, "test.mp4");
Media media = new Media(f.toURI().toURL().toString());
MediaPlayer player = new javafx.scene.media.MediaPlayer(media);
MediaView viewer = new MediaView(player);
player.setOnReady(() -> {
player.setStartTime(Duration.ZERO);
player.setStopTime(media.getDuration().subtract(Duration.valueOf("50")));
});
player.setOnEndOfMedia(() -> player.stop());
StackPane root = new StackPane();
root.getChildren().add(viewer);
Scene scenes = new Scene(root, 500, 500, Color.BLACK);
stage.setScene(scenes);
stage.setTitle("Riddle Game");
stage.setFullScreen(true);
stage.show();
player.play();
}
}
But when I launch the app, it shows a black window and the video is not played.
I don't understand my mistake
Thanks in advance for your help.
EDIT : Still don't work...

HTTP Live streaming not working in JavaFX application

I am trying to create a media-player which plays local media files/media over HTTP live streaming using JavaFX version 2.0.
I am able to play local media files successfully in my media player. But when I try to play media files over HTTP live streaming, then its not working. I am getting white-blank screen as shown below when I try to play media files using HTTP live streaming.
click here to see the screenshot
And following is my code to play media over HTTP live streaming:
public class RemoteMultipleFileStreaming extends Application {
private static final String MEDIA_URL = "http://youtu.be/uvcUO8DPWfs?list=PLRQuJcU2aZG-aMedJxa7p7ylYmOn5iMlS&t=2";
#Override
public void start(Stage primaryStage) {
GridPane root = new GridPane();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setMaximized(true);
primaryStage.show();
Media media = new Media(MEDIA_URL);
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
MediaView mediaView = new MediaView(mediaPlayer);
mediaView.setFitHeight(500);
mediaView.setFitWidth(500);
root.add(mediaView);
mediaPlayer.play();
}
public static void main(String[] args) {
launch(args);
}
}
Please let me know what needs to be done in order to play media files over HTTP live streaming in media player using JavaFX.
i think you may need to ebmedd the video html source like this example and run it as html on WebView object
WebView webView= new WebView();
webView.getEngine().loadContent("<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/EtwcTIqEOKo\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>" +
"", "text/html");

How to load a local HTML file using WebView in JavaFX

I am looking for a way to display an html file in a different stage once the help button is clicked.
public void handleButtonAction(ActionEvent event) throws IOException {
if (event.getSource() == help) {
stage = (Stage) help.getScene().getWindow();
root = FXMLLoader.load(getClass().getResource("help.fxml"));
WebView browser = new WebView();
Scene helpScene = new Scene(root);
Stage helpStage = new Stage();
helpStage.setTitle("Help Menu");
helpStage.setScene(helpScene);
URL url = getClass().getResource("readme.html");
browser.getEngine().load(url.toExternalForm());
helpStage.show();
}
}
Your code is fine except that you forgot to add the webview to the scene, do
((Pane) helpScene.getRoot()).getChildren().add(browser);

JavaFX WebView: Java not accessible

I am trying to load a webpage into my application. I can get the page to load, however, the website I'm trying to load requires Java to run and the WebView cannot seem to find it on my MacBook. See my stripped code below:
public class LoadPage extends Application {
public static WebView loadPage() {
WebView browser = new WebView();
WebEngine eng = browser.getEngine();
eng.load("http://www.java.com/en/download/installed.jsp");
return browser;
}
#Override
public void start(Stage primaryStage) {
try {
BorderPane pane = new BorderPane();
pane.setCenter(loadPage());
Scene scene = new Scene(pane);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setTitle("WebEditor");
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Now my question is: Is there a workaround for this? Any help is much appreciated :)
No you cannot do that, the WebView currently doesn't support plugin technology ! Please see Jewelsea's answer over here !
Javafx: Java applet in a Webview component

JavaFX video not playing

I followed some tutorials about combining JavaFX with Swing (JFrame) to play a video, however all I get is a black screen where the video is supposed to be without any actual content playing, No errors are reported either.
What am I doing wrong here and why wont the video play?
I tried several .flv videos, none of them will start playing (they do play when I open them in my browser)
I'm running jre7 and jdk1.7.0_45 on windows 8.1 N Pro with the K-lite full codec pack installed
EDIT: updated my code after the comment of jewelsea, nothing has changed, the black box still appears without content playing, the console doesn't show any text coming up
package com.example.test;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.media.Media;
import javafx.scene.media.MediaErrorEvent;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Color;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
initAndShowGUI();
}
});
}
private static void initAndShowGUI() {
// This method is invoked on the EDT thread
JFrame frame = new JFrame("Test");
final JFXPanel fxPanel = new JFXPanel();
frame.add(fxPanel);
frame.setSize(640, 480);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Platform.runLater(new Runnable() {
#Override
public void run() {
initFX(fxPanel);
}
});
}
private static void initFX(JFXPanel fxPanel) {
// This method is invoked on the JavaFX thread
Scene scene = createScene();
fxPanel.setScene(scene);
}
private static Scene createScene() {
String source;
Media media;
MediaPlayer mediaPlayer;
MediaView mediaView = null;
try {
media = new Media("http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv");
if (media.getError() == null) {
media.setOnError(new Runnable() {
public void run() {
// Handle asynchronous error in Media object.
System.out.println("Handle asynchronous error in Media object");
}
});
try {
mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
if (mediaPlayer.getError() == null) {
mediaPlayer.setOnError(new Runnable() {
public void run() {
// Handle asynchronous error in MediaPlayer object.
System.out.println("Handle asynchronous error in MediaPlayer object");
}
});
mediaView = new MediaView(mediaPlayer);
mediaView.setOnError(new EventHandler() {
public void handle(MediaErrorEvent t) {
// Handle asynchronous error in MediaView.
System.out.println("Handle asynchronous error in MediaView: "+ t.getMediaError());
}
#Override
public void handle(Event arg0) {
// TODO Auto-generated method stub
System.out.println("Handle asynchronous error in MediaView arg0: "+arg0.toString());
}
});
} else {
// Handle synchronous error creating MediaPlayer.
System.out.println("Handle synchronous error creating MediaPlayer");
}
} catch (Exception mediaPlayerException) {
// Handle exception in MediaPlayer constructor.
System.out.println("Handle exception in MediaPlayer constructor: "+ mediaPlayerException.getMessage());
}
} else {
// Handle synchronous error creating Media.
System.out.println("Handle synchronous error creating Media");
}
} catch (Exception mediaException) {
// Handle exception in Media constructor.
System.out.println("Handle exception in Media constructor: "+mediaException.getMessage());
}
Group root = new Group();
Scene scene = SceneBuilder.create().width(640).height(480).root(root).fill(Color.WHITE).build();
if(mediaView != null) {
root.getChildren().add(mediaView);
}
return scene;
}
}
So I installed the windows media feature pack in order to get adobe premiere pro working (because it required a dll file from windows media player (which I didn't had installed because I run an N version of windows) and now the video does play for me.
I can't say with 100% confirmation the cause was not having WMP installed as the media feature pack might as well have installed something else that solved my problem, nonetheless, problem solved :)
I want to thank the other answers for trying, I really appreciate it.
Please do not mind if i am writing this answer. I know this is a very old question but this answer might help others. I am currently developing a JavaFX application which needs to execute a file depending upon its type.
My application played the video for the first time but when i clicked on another mp4 video file it didn't play.
Here is my initial code.
private void playVideo(String fileLocation) {
System.out.println("VideoProcesser Thread = " + Thread.currentThread().getName());
media = new Media(new File(fileLocation).toURI().toString());
mediaPlayer = new MediaPlayer(media);
mediaView = new MediaView(mediaPlayer);
runnable = () -> {
System.out.println("Inside runnable VideoProcesser Thread = " + Thread.currentThread().getName());
mediaPlayer.play();
};
mediaPlayer.setOnReady(runnable);
setVideoMediaStatus(PLAYING);
pane.getChildren().add(mediaView);
}
Then since the video player screen was dark, i thought the problem was with media view, so i added the following two line,
if(mediaView == null) {
mediaView = new MediaView(mediaPlayer);
}
mediaView.setMediaPlayer(mediaPlayer);
Now, when i click on different videos my application just plays fine.
Here is the complete code.
Media media;
MediaPlayer mediaPlayer;
MediaView mediaView;
private void playVideo(String fileLocation) {
System.out.println("VideoProcesser Thread = " + Thread.currentThread().getName());
media = new Media(new File(fileLocation).toURI().toString());
mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
if(mediaView == null) {
mediaView = new MediaView(mediaPlayer);
}
mediaView.setMediaPlayer(mediaPlayer);
mediaPlayer.play();
mediaPlayer.setOnError(() -> System.out.println("Current error: "+mediaPlayer.getError()));
setVideoMediaStatus(PLAYING);
pane.getChildren().add(mediaView);
}
Note that if you are using FXML to instantiate mediaView, then do not instantiate it again. Instantiating it again might make mediaView loose the reference of original node.
Refer to this post and answer by itachi,
javafx mediaview only the audio is playing
try this, it works for me:
package de.professional_webworkx.swing;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javax.swing.JFrame;
public class MyFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Create a new Frame, set title, ...
*/
public MyFrame() {
this.setTitle("Swing and JavaFX");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(1024, 768);
// create a JFXPanel
final JFXPanel jfxPanel = new JFXPanel();
// add the jfxPanel to the contentPane of the JFrame
this.getContentPane().add(jfxPanel);
this.setVisible(true);
Platform.runLater(new Runnable() {
#Override
public void run() {
jfxPanel.setScene(initScene());
}
});
}
public static final void main (String[] args) {
new MyFrame();
}
/**
* init the JFX Scene and
* #return scene
*/
private Scene initScene() {
Group root = new Group();
SceneBuilder<?> sb = SceneBuilder.create().width(640).height(400).root(root);
Media video = new Media("http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv");
MediaPlayer mediaPlayer = new MediaPlayer(video);
mediaPlayer.setAutoPlay(true);
mediaPlayer.play();
MediaView view = new MediaView(mediaPlayer);
root.getChildren().add(view);
Scene scene = sb.build();
return scene;
}
}
Patrick
I took your code and tried running it on my machine (Win7 JDK 1.7.0_25) and got the same results. Black box and no video.
I noticed you aren't setting mediaPlayer.setAutoPlay(true) so I added that call to the createScene() right before mediaPlayer is passed to MediaView. Now the playback seems to work for me.
// ... prior code omitted
// added this to OP's code
mediaPlayer.setAutoPlay(true);
mediaView = new MediaView(mediaPlayer);
mediaView.setOnError(new EventHandler() {
public void handle(MediaErrorEvent t) {
// Handle asynchronous error in MediaView.
System.out.println("Handle asynchronous error in MediaView: "+ t.getMediaError());
}
// ... additional code omitted
Edit: The autoPlay property defaults to false - you can call mediaPlayer.isAutoPlay() to check this. Without either calling mediaPlayer.setAutoPlay(true) or mediaPlayer.play() the video will never start playing.
Edit 2: I just noticed in the comments on another answer that you are having trouble playing the video outside of JavaFX. If you don't already have it installed, try downloading VLC to see if the video can play using that. I believe installing ffdshow tryouts will provide the necessary codecs to play FLV in Windows Media Player. (Although I thought all versions of K-lite codec pack included FLV support)

Categories