I've been using MediaPlayer on win10 and it works perfectly fine, but on my MacBook Air (10.13.4 (17E202)) is no sound output at all. I've already found similar problems and observed that its caused by the GC.
I tried everything avoiding this, based on answers like this one
I tried making the media player final, static, private, public, working with getter and setter, outsourcing, as a class var, as a local var but nothing is working.
I'm running on Java 9.0.4.
If I add player.setCycleCount(MediaPlayer.INDEFINITE); the sound plays, but its looping and if I set it to 2 its working well, but my next , back, play, pause buttons are getting very weird then.
Using AudioClip instead is no option for me, because I have to use the MediaPlayer .
public class Main extends Application {
MediaPlayer player;
#Override
public void start(Stage primaryStage) throws Exception {
BorderPane root = new BorderPane();
File file = new File("/users/xxx/desktop/xxx/Song.mp3");
Media media = new Media(file.toURI().toString());
this.player = new MediaPlayer(media);
player.play();
primaryStage.setScene(new Scene(root, 600, 400));
primaryStage.show();
}
}
I would appreciate every help.
Try adding player.setStartTime(new Duration(0)); before player.play()
I solved the problem. I had to put player.stop(); before the player.start(); call.
¯\_(ツ)_/¯
Related
I've started a class on Java, and we've gotten to JavaFX. I'm having trouble getting an image to display. After a little debugging, I was able to narrow it down to the following exception:
com.sun.javafx.iio.ImageStorageException: No loader for image data
I know the path name is correct, as that was the first error/exception I was able to fix. The current GUI shows up blank, as if I've put nothing in the scene. I can add other aspects (such as labels), but the image never loads.
public class Main extends Application{
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
Image Belial = new Image("file:C:\\Users\\jakem\\Desktop\\D&D Stuff\\Belial.jpg");
System.out.println("Image loaded? " + !Belial.isError());
if (Belial.isError()) {
System.out.println(Belial.getException());
}
ImageView imageView = new ImageView(Belial);
HBox hbox = new HBox(imageView);
Scene scene = new Scene(hbox);
primaryStage.setScene(scene);
primaryStage.setTitle("Belial");
primaryStage.show();
}
These are the modules I've loaded, along with adding the JavaFX library to my project library.
--module-path ${PATH_TO_FX} --add-modules javafx.controls,javafx.fxml
I'm using Intellij.
EDIT: As it turns out, my image file was not actually in jpg format despite having a jpg extension. Switched to a properly formatted jpg and it worked fine.
If I add some media (pictures and/or sounds) my JavaFX artifact won't launch.
Im using macOS and IntelliJ. Launching the .jar from Terminal returns "Error: Could not find or load main class". However, this only occurs with pictures I stored in variables but haven't included yet (for ex. a PlayerIcon) so it isn't a manifest issue.
I installed Java 8 because I got so many issues with Java 10, 11 and 12. Inside of IntelliJ the project launches normally, just the exported .jar is affected of the issue. I am new to JavaFX and I did not include a FXML file. Maybe that's the issue?
Heres a code snippet of how I included one image:
Image Scoreboard = new Image(getClass().getResourceAsStream("Scoreboard.png"));
ScoreBoardContainer.setImage(Scoreboard);
Group root = new Group();
root.getChildren().add(ScoreBoardContainer);
Scene GameUI = new Scene(root, w, h);
Thanks for your help!
Longer code snippet:
package sample;
import ...
public class Main extends Application {
...variables...
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
final ImageView HintergrundMuster = new ImageView();
final ImageView ScoreBoardContainer = new ImageView();
Image HG = new Image(getClass().getResourceAsStream("space-background.png"));
Image PlanetBraun = new Image(getClass().getResourceAsStream("Planet.png"));
Image Scoreboard = new Image(getClass().getResourceAsStream("Scoreboard.png"));
HintergrundMuster.setImage(HG);
ScoreBoardContainer.setImage(Scoreboard);
AudioClip SprungSound = new AudioClip(new File("jump.mp3").toURI().toString());
SprungSound.play(0);
Canvas canvas = new Canvas(b,h);
Vordergrund = canvas.getGraphicsContext2D();
Group root = new Group();
root.getChildren().add(HintergrundMuster);
root.getChildren().add(canvas);
root.getChildren().add(ScoreBoardContainer);
Scene GameUI = new Scene(root, b, h);
primaryStage.setTitle(„Test“);
primaryStage.setScene(GameUI);
primaryStage.show();
...
I just found the solution to my problem. Thanks for your help anyway!
I overlooked that some of my files had the .PNG format while others had .png. For IntelliJ, this doesn't matter, but for the JAR compiler it does indeed matter apparently.
Also, I had a problem with my audio files so I copied them into my "sample" folder and changed the code a little bit. Following snipped worked for me:
AudioClip SprungSound = new AudioClip(this.getClass().getResource("jump.mp3").toString());
When trying to show an image stored in the system clipboard, I ran into this problem: the resulting picture is always bigger than the original and a bit blurry.
Here's what I used (in Scala):
No, my code is exactly the same, it's just in Scala (I also tried running the Java version, the same result)
import javafx.application.Application
import javafx.scene.Scene
import javafx.scene.image.ImageView
import javafx.scene.input.Clipboard
import javafx.scene.layout.StackPane
import javafx.stage.Stage
object Test extends App{
Application.launch(classOf[HelloWorld], args: _*)
}
class HelloWorld extends Application {
override def start(primaryStage: Stage) {
primaryStage.setTitle("Hello World!")
val root = new StackPane
val image = Clipboard.getSystemClipboard().getImage()
val imageView = new ImageView(image)
root.getChildren.add(imageView)
primaryStage.setScene(new Scene(root, 300, 250))
primaryStage.show
}
}
Original picture and what ImageView makes of it:
http://imgur.com/AFJudkL
I've also tried saving the image to a file and it's still distorted, so the problem is not in ImageView.
Even had a wild idea to call SetProcessDPIAware from WinAPI, had absolutely no effect.
Is there any way to show the image without distortions?
Thanks!
UPDATE: I've provided the full version of the code (It's exactly the same as the Java version below. I'm running it on Windows 10, jdk1.8.0_131, DPI is set to 175%.
I'm more and more certain that the problem I'm having is because of DPI - the image ImageView is showing is exactly 1.75 times larger that the original. Now I can force it to be of the right size with the following trick:
imageView.setFitHeight(image.getHeight/1.75)
imageView.setFitWidth(image.getWidth/1.75)
But it's still blurry. Also I found a great blog post that deals with that problem (different aspect of it, though - snapshot taking). However, I have no idea how to apply this solution to my conundrum.
http://news.kynosarges.org/2017/02/01/javafx-snapshot-scaling/
You should try to set the preserveRatio property of the image view.
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/ImageView.html#preserveRatioProperty
Update:
I converted your code fragment into an executable example and when I run this on my Mac (Retina) I don't see any distortion at all. So, how is your code (which you didn't provide) different from mine and what are the relevant characteristics of your Windows box?
public class ClipboardBug extends Application {
public static void main(String[] args) {
Application.launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Hello World!");
StackPane root = new StackPane();
Image image = Clipboard.getSystemClipboard().getImage();
ImageView imageView = new ImageView(image);
imageView.setPreserveRatio(true);
root.getChildren().add(imageView);
primaryStage.setScene(new Scene(root));
primaryStage.sizeToScene();
primaryStage.show();
}
}
I'm working on JavaFX 8 app and I'm trying to open MP3 file using MediaPlayer.
I had passed wrong URL errors and I've no exceptions right now, when I start such part of code, but app opens and there is no sound. Tried with some oracle tutorial and when i put such URL: "http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv" everything is working so I guess it's still wrong URL, but app is starting and I've litterally no clue whats wrong.
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
//Add a scene
Group root = new Group();
Scene scene = new Scene(root, 500, 200);
File file = new File("C:\\Users\\Me\\Desktop\\SomeFile.mp3");
Media media = new Media(file.toURI().toASCIIString());
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);
//show the stage
primaryStage.setTitle("Media Player");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Considering the file is correctly located and loaded you can do the following:
file.toURI().toURL().toExternalForm()
If you are writing an app that plays various media files, consider using user interface to obtain paths to external file resources instead of hardcoding them. You can use FileChooser or DirectoryChooser
I currently have some code that makes a button in the primaryStage that spawns a new stage. My goal is to have the button close the stage it's on using the setOnMouseClicked method right after launching the new one. Here is how it's currently setup:
#Override
public void start(Stage primaryStage) {
setPlayBtn();
}
private void setPlayBtn() {
play = new ImageView(new Image(BugWars.class.getResourceAsStream("images/play-btn.png")));
play.setFitHeight(50);
play.setFitWidth(50);
play.setX(375);
play.setY(375);
play.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent t) {
setGame(); // This creates the new stage.
primaryStage.close();
}
});
Unfortunately this doesn't work. Netbeans complains that it can't find the symbol. It thinks it's a variable. I'm sure it's something stupid, but any help referencing the primaryStage would be appreciated. Thanks guys!
So I've worked around the problem by simply making the PlayBtn instatiate inside of the start() method that(I believe) creates primaryStage and then making primaryStage final. I don't know why this works, but it does.