I am just trying to play a mp3 file using JavaFX with JDK 10.
I only have 1 main Java file. The code works with JDK 8 but not JDK 10. When running with JDK 10, I compiled with the --add-modules=javafx.graphics,javafx.media flag and it can run, showing the window but no audio is being played.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.media.Media;
import javafx.scene.Scene;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import java.io.File;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Hello World");
Group root = new Group();
Scene scene = new Scene(root, 300, 275);
primaryStage.setScene(scene);
primaryStage.sizeToScene();
primaryStage.show();
String bip = "bip.mp3";
Media hit = new Media(new File(bip).toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(hit);
mediaPlayer.setAutoPlay(true);
MediaView mediaView = new MediaView(mediaPlayer);
((Group)scene.getRoot()).getChildren().add(mediaView);
}
public static void main(String[] args) {
launch(args);
}
}
How to make MediaPlayer work with JDK 10? Otherwise, any idea what may be the cause of this issue?
Related
I keep getting an ERROR_MEDIA_INVALID error when trying to play some .mp3 files via the JavaFX media player. Previous google searches have just led to old bug reports, and I'm not sure if this an actual bug or something I missed.
Example:
package sample;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
import java.io.File;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("Hello World!");
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
//change filename as needed
var file = new File("C:/Users/tgaravaglia/Downloads/test.mp3");
var media = new Media(file.toURI().toString());
var player = new MediaPlayer(media);
player.setOnError(new Runnable()
{
#Override
public void run()
{
//ERROR_MEDIA_INVALID here
player.getError().printStackTrace();
}
});
player.play();
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Stack trace:
MediaException: UNKNOWN : [com.sun.media.jfxmediaimpl.platform.gstreamer.GSTMediaPlayer#6f4b481f] ERROR_MEDIA_INVALID: ERROR_MEDIA_INVALID
at javafx.media/javafx.scene.media.MediaException.getMediaException(MediaException.java:160)
at javafx.media/javafx.scene.media.MediaPlayer$_MediaErrorListener.onError(MediaPlayer.java:2623)
at javafx.media/com.sun.media.jfxmediaimpl.NativeMediaPlayer$EventQueueThread.HandleErrorEvents(NativeMediaPlayer.java:692)
at javafx.media/com.sun.media.jfxmediaimpl.NativeMediaPlayer$EventQueueThread.run(NativeMediaPlayer.java:426)
Working file: https://drive.google.com/file/d/1e3k9gVhV_hDehWHwhHElXC2jU4aKn6oc/view?usp=sharing
Broken file: https://drive.google.com/file/d/1_VKx4zLH6lFLv6VJdDvrP6IP3c-TjOYo/view?usp=sharing
So... I figured it out. That bad "mp3" file is actually a .wav file in disguise. I ran it through a cloud-based wav->mp3 converter and now it works fine. Hopefully this helps someone in the future with the same problem!
I have recently started on intelij a beginner in Javafx. I am getting this symbol error for stackpane and i have no idea why am i getting it. I have tried switching my jdk between 8, 9, and 10 still have the same problem.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import javafx.scene.StackPane;
public class JavaFX1 extends Application {
#Override
public void start(Stage primaryStage) {
Button button = new Button("Click Here");
primaryStage.setTitle("The Button");
StackPane layout = new StackPane();
layout.getChildren.add(button);
Scene scene = new Scene(layout, 300, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String args[]){
Application.launch(args);
}
}
Try changing
this:layout.getChildren.add(button); to this:layout.getChildren().add(button);
I want to implement file drag and upload in java webview, however, it keeps crashing JVM when any type of upload is initiated. Tested on latest JDK 8 and 9 on windows 10. I'm lost as to what can be causing this issue.
import javafx.application.Application;
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 DropFileUpload extends Application {
public static void main(String[] args) {
Application.launch(args);
}
#Override
public void start(final Stage stage) {
WebView webView = new WebView();
final WebEngine webEngine = webView.getEngine();
//webEngine.load("https://html5demos.com/dnd-upload/");
webEngine.load("http://www.dropzonejs.com");
VBox root = new VBox();
root.getChildren().add(webView);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
I have a problem using custom Fonts in Javafx 8.
Whenever I try to display a text it gets convertet to upper case A's
my code goes as follows:
package main;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class Main extends Application {
private Canvas can;
private GraphicsContext gc;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
Pane root = new Pane();
Scene scene = new Scene(root, 800, 400);
stage.setTitle("Test");
can = new Canvas(scene.getWidth(), scene.getHeight());
gc = can.getGraphicsContext2D();
root.getChildren().add(can);
stage.setResizable(false);
stage.setScene(scene);
stage.show();
InputStream is = Main.class.getResourceAsStream("Font/SSF4ABUKET.ttf");
Font font = Font.loadFont(is, 30);
gc.setFont(font);
gc.setFill(Color.RED);
gc.fillText("This is a test", 10, 200);
}
}
If I try this Font in a normal Text Editor (e.g. Open Office) it works perfectly fine.
Thanks for your help in advance.
I was trying to play some audio and video files on a laptop usingJavaFX MediaPlayer and MediaView but the media didn't work and also the application didn't give any exceptions, but when i moved the application to another laptop it worked well
Both Laptops are HP and working on MS Widows 8 OS, under JDK 8u102
here's the source code
import java.io.File;
import java.net.MalformedURLException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
/**
*
* #author Anoos
*/
public class Task1 extends Application {
#Override
public void start(Stage primaryStage) throws MalformedURLException {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
try {
Media media
= new Media(new File("002.mp3").
toURI().toURL().toString());
System.out.println(media.getSource());
MediaPlayer player = new MediaPlayer(media);
player.play();
} catch (Exception e) {
e.printStackTrace();
}
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}}
so, can any one help me please ?
and thank you in advance (: