Create a Movie Player in javafx using eclipse - eclipse

I want to create a Movie Player in javafx using eclipse.My code is compile successfully but it gives run time error.I tried using different file path also.
But it not resolved the error.
My code is
package Player3;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class MediaPlayer3 extends Application{
public static void main(String args[])
{
launch(args);
}
public void start(Stage stage) throws Exception {
Group root = new Group();
Media media =new Media("file:////‪C://Kaise.MP4");
MediaPlayer player4=new MediaPlayer(media);
MediaView view = new MediaView(player4);
root.getChildren().add(view);
Scene scene =new Scene(root,400,400,Color.BLACK);
stage.setScene(scene);
stage.show();
player4.play();
}
}
The error is
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
java.lang.RuntimeException: Exception in Application start method
java.lang.RuntimeException: Exception in Application start method

As #Sergei Sirik mentioned this is JavaFX and not 'pure' java. Anyway reading the documentation of Media class for the constructor(String source) you will see :
The source must represent a valid URI and is immutable. Only HTTP, FILE, and JAR URLs are supported. If the provided URL is invalid then an exception will be thrown
So I will suggest first to create a File object ( to be able to check file permissions for read write etc ) and after that just pass the mediaFile.toURI().toString() to the Media class constructor and it will open.
Edit :
I guess you will use a FileChooser in future in order to load your video so it will make the file creation and handling much easier.
I had test the code below and it loads successfully my videos on my computer.
import java.io.File;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String args[]) {
launch(args);
}
public void start(Stage stage) throws Exception {
Group root = new Group();
FileChooser fc = new FileChooser();
File x = fc.showOpenDialog(null);
Media media = new Media(x.toURI().toString());
MediaPlayer player4 = new MediaPlayer(media);
MediaView view = new MediaView(player4);
root.getChildren().add(view);
Scene scene = new Scene(root, 400, 400, Color.BLACK);
stage.setScene(scene);
stage.show();
player4.play();
}
}
If you have any error like :
MediaException: MEDIA_UNSUPPORTED : Unrecognized file signature!
It's probably cause you alter the file signature by hand or try to play an unsupported file format like mkv.

Related

JWebPane cannot be resolved to a type

I am having troubles with javafx , eclipse keeps showing this error :
Description Location Resource Path Type
JWebPane cannot be resolved to a type line 20 Browser.java /web
browser/src Java Problem
I am not sure where I have went wrong and the error does not show until I compile the app.
here is my simple code :
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class Browser {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebView browser = new WebView();
WebEngine eng = browser.getEngine();
eng.load("http://google.com");
VBox vb = new VBox();
vb.getChildren().add(browser);
Scene scene = new Scene(vb, 500, 200);
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
}
}

JFXPanel throws UnsatisfiedLinkError in java-9 - how to fix?

Just tried to run my old tests in java-9 and see them not running at all due to an exception thrown by the code that guarantees running on the FX-threaad (the ol' trick to instantiate a JFXPanel)
The stand-alone example below (it's the plain tutorial code) throws it as well:
Exception in thread "main" java.lang.UnsatisfiedLinkError: D:\java\jdk\190_ea\bin\awt.dll: Can't find dependent libraries
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1935)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1841)
at java.lang.Runtime.loadLibrary0(Runtime.java:874)
at java.lang.System.loadLibrary(System.java:1770)
at java.awt.Toolkit$3.run(Toolkit.java:1355)
at java.awt.Toolkit$3.run(Toolkit.java:1353)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Toolkit.loadLibraries(Toolkit.java:1352)
at java.awt.Toolkit.<clinit>(Toolkit.java:1387)
at java.awt.EventQueue.invokeLater(EventQueue.java:1268)
at javax.swing.SwingUtilities.invokeLater(SwingUtilities.java:1381)
at de.swingempire.fx.swing.JFXPanelExample.main(JFXPanelExample.java:59)
Environment is win7, jdk9-ea-107 (without jigsaw), eclipse-neon-ea - questions are simple: a) what's wrong exactly, b) how to fix?
The exact output of java -version is:
java version "9-ea" Java(TM) SE Runtime Environment (build 9-ea+107-2016-02-24-175644.javare.4520.nc)
Java HotSpot(TM) Client VM (build 9-ea+107-2016-02-24-175644.javare.4520.nc, mixed mode)
The code:
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
public class JFXPanelExample {
private static void initAndShowGUI() {
// This method is invoked on the EDT thread
JFrame frame = new JFrame("Swing and JavaFX");
final JFXPanel fxPanel = new JFXPanel();
frame.add(fxPanel);
frame.setSize(300, 200);
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() {
Group root = new Group();
Scene scene = new Scene(root, Color.ALICEBLUE);
Text text = new Text();
text.setX(40);
text.setY(100);
text.setFont(new Font(25));
text.setText("Welcome JavaFX!");
root.getChildren().add(text);
return (scene);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
initAndShowGUI();
}
});
}
}
Update
tried both suggestions in the comments (updating to most recent 9-ea-107, running from the command line) - no success, same exception.
Another observation: the example above fails with the same stacktrace even when all fx related code is commented - plain swing won't run. Looks like something severely wrong in my environment.

Error in JavaFX FXML tutorial

I have followed every step in this (https://blogs.oracle.com/jmxetc/entry/connecting_scenebuilder_edited_fxml_to) tutorial, and yet keep getting an error about this section in the controller class:
myButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("That was easy, wasn't it?");
}
});
The only difference I can think of is that it was written for FX 2.0 and not 8.0?
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.Error: Unresolved compilation problems:
The method setOnAction(EventHandler<ActionEvent>) in the type ButtonBase is not applicable for the arguments (new EventHandler<ActionEvent>(){})
EventHandler cannot be resolved to a type
The method handle(ActionEvent) of type new EventHandler<ActionEvent>(){} must override or implement a supertype method
at simple.SimpleController.initialize(SimpleController.java:24)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at simple.Main.start(Main.java:26)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application simple.Main
Let me know if you need to see the classes and FXML files (dw, they're very short)
Thank you!
PS- Please bear with me if I need clarification about your answer because as a Java newbie I often get confused!
EDIT 1:
package simple;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SimpleController
implements Initializable {
#FXML // fx:id="myButton"
private Button myButton; // Value injected by FXMLLoader
#Override // This method is called by the FXMLLoader when initialization is complete
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
assert myButton != null : "fx:id=\"myButton\" was not injected: check your FXML file 'simple.fxml'.";
// initialize your logic here: all #FXML variables will have been injected
myButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("That was easy, wasn't it?");
}
});
}
}
You are missing the import for EventHandler, and you have the wrong import for ActionEvent. (Also you have imported ActionListener for some reason, though you never use it.)
Your imports should be
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;

hadoop mapreduce wordcount program

I tried to run a word count program in eclipse. in the browser the output directories are being created but i am not getting any out put. I am getting the following error. plz help me out. please. I am struct at the last point.
I gave the following command to execute the program.
hduser#niki-B85M-D3H:/home/niki/workspace$ hadoop jar wordcount.jar WordCount /user/hadoop/dir1/file.txt wordcountoutput
The output file named wordcountoutput is created but the error is displayed as follows.
I tried to run a word count program in eclipse. in the browser the output directories are being created but i am not getting any out put. I am getting the following error. plz help me out. please. I am struck at the last point.
I gave the following command to execute the program.
hduser#niki-B85M-D3H:/home/niki/workspace$ hadoop jar wrd.jar WordCount /user/hduser/outputwc /user/hduser/outputwc/
The output file named wordcountoutput is created but the error is displayed as follows.
5/12/24 15:15:46 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at WordCount.run(WordCount.java:29)
at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:70)
at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:84)
at WordCount.main(WordCount.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.hadoop.util.RunJar.run(RunJar.java:221)
at org.apache.hadoop.util.RunJar.main(RunJar.java:136)
this is the exception i am getting now at the final step
WordCount.java
import java.io.IOException;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider.Text;
public class WordCount extends Configured implements Tool{
#Override
public int run(String[] args) throws Exception {
if(args.length<2){
System.out.println("plz give input and output directories");
return -1;
}
JobConf conf = new JobConf();
//conf.setJarByClass(WordCount.class);
conf.setJar("wrd.jar");
FileInputFormat.setInputPaths(conf,new Path(args[1]));
FileOutputFormat.setOutputPath(conf,new Path(args[2]));
conf.setMapperClass(WordMapper.class);
conf.setReducerClass(WordReducer.class);
conf.setMapOutputKeyClass(Text.class);
conf.setMapOutputValueClass(IntWritable.class);
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);
JobClient.runJob(conf);
return 0;
}
public static void main(String args[]) throws Exception{
int exitCode =ToolRunner.run(new WordCount(), args);
System.exit(exitCode);
}
}
Word Reducer.java
import java.io.IOException;
import java.util.Iterator;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
public class WordReducer extends MapReduceBase implements Reducer<Text,IntWritable,Text,IntWritable>{
#Override
public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter r)
throws IOException {
int count=0;
while(values.hasNext()){
IntWritable i= values.next();
count+=i.get();
}
output.collect(key,new IntWritable(count));
}
}
WordMapper.java
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;
public class WordMapper extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
#Override
public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter r)
throws IOException {
String s = value.toString();
for(String word:s.split(" ")){
if(word.length()>0){
output.collect(new Text(word), new IntWritable(1));
}
}
}
}
Try this out:
public static void main(String args[]) throws Exception{
int exitCode =ToolRunner.run(new Configuration(), new WordCount(), args);
System.exit(exitCode);}
according to this

how can i use Gstreamer in javafx videoplayer?

this is a videoplayer in javafx.how we can support mkv,vob,avi etc extension ?
is this possible to use gstreamer in javafx to support other extension?
how can we use gstreamer or if not then plz say any other way to make the videoplayer other extension supported...
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class MoviePlayer extends Application {
/**
*
*/
public static void main(String args[])
{
launch(args);
}
#Override
public void start(Stage arg0) throws Exception {
final Stage stage=new Stage();
stage.setTitle("Video Player");
Group root = new Group();
Media media = new Media("file:///C:/Users/vi/downloads/Video/a.mp4");
final MediaPlayer player=new MediaPlayer(media);
MediaView view=new MediaView(player);
// System.out.println("media.Width"+media.getWidth());
final VBox vbox=new VBox();
Slider slider=new Slider();
root.getChildren().add(view);
root.getChildren().add(vbox);
root.getChildren().add(slider);
Scene scene=new Scene(root, 400,400,Color.BLACK);
stage.setScene(scene);
stage.show();
player.play();
player.setOnReady(new Runnable() {
#Override
public void run() {
int w=player.getMedia().getWidth();
int h=player.getMedia().getHeight();
stage.setMinWidth(w);
stage.setMinHeight(h);
vbox.setMinSize(w,100 );
vbox.setTranslateY(h);
}
});
}
}
Old topic, but stumbling around today brought up a gstreamer-java repo on GitHub. It's a few years old. But lately there are fresh gst1-java and gst1-java-fx projects there. As in updated 11 days ago. It would be interesting to try out the examples and see if they can accept other movie formats. But it looks like the documentation is sparse to none.
Another alternative is to use ffmpeg or handbrake to remux the videos into another container format.