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!
Related
I was testing the MonologFX from JFXtras (v8.0-r5), but I got stuck with it!
Can anyone tell me how to check what was the button in the dialog that was pressed by the user? I tried in many ways, but no luck at all.
package javafx_jfxtras_monologfx;
import javafx.application.Application;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
import jfxtras.labs.dialogs.MonologFX;
import jfxtras.labs.dialogs.MonologFXButton;
import jfxtras.labs.dialogs.MonologFX.Type;
public class JavaFX_JFXtras_MonologFX extends Application
{
#Override
public void start(Stage stage)
{
MonologFX m = new MonologFX();
m.setModal(true);
m.setType(Type.QUESTION);
m.setTitleText("JFXtras MonologFX");
m.setMessage("Do you want to continue?");
m.setPos(698, 450);
MonologFXButton mb1 = new MonologFXButton();
mb1.setType(MonologFXButton.Type.YES);
mb1.setLabel("Continue");
m.addButton(mb1);
MonologFXButton mb2 = new MonologFXButton();
mb2.setType(MonologFXButton.Type.NO);
mb2.setLabel("Exit");
m.addButton(mb2);
Button btn = new Button();
btn.setText("Click the Button");
btn.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
System.out.println("Hello :)");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
stage.setTitle("JavaFX - JFXtras MonologFX");
stage.setScene(scene);
stage.show();
m.show();
}
public static void main(String[] args)
{
launch(args);
}
}
But the controls in labs are experimental and Mark has not worked on this one for a long time. We don't take them out because someone may use them, but as of version 8u40 JavaFX has a dialog itself. https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Dialog.html|
This question already has an answer here:
How to load images from URL in JavaFx if recieving HTTP Error 403 [duplicate]
(1 answer)
Closed 5 years ago.
I've been trying for a while now, following various documentations but I just cannot get any images to show up on JavaFX.
Here is my code:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
//stage and stuff
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
//images
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class KingsGame extends Application {
public static ImageView iv = new ImageView();
Button btn = new Button();
public static Image test = new Image("http://puu.sh/vOk8i/754c8fee68.png");
#Override
public void start(Stage primaryStage) {
//stackpane
StackPane root = new StackPane();
root.getChildren().add(iv);
//scene
Scene scene = new Scene(root, 1280, 720);
primaryStage.setTitle("test program lol");
primaryStage.setScene(scene);
primaryStage.show();
//---actual game---
drawMainMenu();
}
public void helloTest() {
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
}
public static void drawMainMenu() {
iv.setImage(test);
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Whenever I run the program, all I get is this: http://puu.sh/vOko6/b669cfc20b.png
The strange thing is, when I initially tested this, I used this (https://osu.ppy.sh/ss/8062698) image link as my test image and it somehow worked even though there wasn't even a .jpg or .png extension. It was a game screenshot and I simply just used the link the game gave me to test. When I switched it to another test link, it just broke.
How can I get ALL image links to work?
It looks like an access problem.
If you print the exception returned with this instruction :
System.err.println(test.getException());
you get this :
java.io.IOException: Server returned HTTP response code: 403 for URL: http://puu.sh/vOk8i/754c8fee68.png
The site probably authorizes only the browser clients
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 (:
I want my java application such that if user chooses to click on a button the PDF opens using the default PDF reader that is installed in the computer.
The PDF which i want to be opened is present in same package "application".
The code which I am using is
package application;
import java.io.File;
import javafx.application.Application;
import javafx.application.HostServices;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(final Stage primaryStage) {
Button btn = new Button();
btn.setText("Load PDF");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
File pdfFile = new File("computer_graphics_tutorial.pdf");
getHostServices().showDocument(pdfFile.toURI().toString());
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
If the PDF file is in the same package as the caller file (as you state), then
getHostServices().showDocument(getClass()
.getResource("computer_graphics_tutorial.pdf").toString());
should solve the problem.
The getResource method can be used really flexibly to locate files. Here is a small description how to use it: JavaFX resource handling: Load HTML files in WebView.
I am trying to learn JavaFX, and I've written the code shown down below, however I seem to be having trouble with this line of code:
btn.setOnAction(new EventHandler<ActionEvent>()
where it underlines setOnAction, and prints this Error:
The method setOnAction(EventHandler<ActionEvent>) in the type ButtonBase is not applicable for the arguments (new EventHandler<ActionEvent>(){})
import java.awt.event.ActionEvent;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Test extends Application{
public static void main(String[] args){
launch(args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
Button btn = new Button();
btn.setText("Say 'Hello World' ");
btn.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent event) {
System.out.println("Button clicked");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}
What am I doing wrong?
You have imported awt event listener just change this line of code
import java.awt.event.ActionEvent;
with this
import javafx.event.ActionEvent;
and you can also use lambda expression like this
btn.setOnAction((event) -> {
System.out.println("Button clicked");
});
You're mixing up Javafx with Swing. Replace
import java.awt.event.ActionEvent;
with
import javafx.event.ActionEvent;