3
import java.io.File;

import javafx.application.Application;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;

public class SoundTest extends Application{
public static void main(String[] args) {
    launch(args);
}

    public static void sound() {
        String path = "test.mp3";
        Media media = new Media(new File(path).toURI().toString());
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        mediaPlayer.play();
    }

    @Override
    public void start(Stage arg0) throws Exception {
        sound();
    }
}

I have some issues. I googled and stumbled across a few helpful stackoverflow posts which provided explanations how sounds are loaded via media and media player.

What I am doing is, Im calling the sound function in the main() but my program fails to execute due to some failure in the second like of my sound function. The media object accepts an argument in the constructor which is the path to the audio file. Somehow it fails there as I get:

Exception in thread "Thread-0" java.lang.IllegalStateException: Toolkit not initialized
    at com.sun.javafx.application.PlatformImpl.runLater(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.runLater(Unknown Source)
    at javafx.application.Platform.runLater(Unknown Source)
    at javafx.scene.media.Media$_MetadataListener.onMetadata(Unknown Source)
    at com.sun.media.jfxmediaimpl.MetadataParserImpl.done(Unknown Source)
    at com.sun.media.jfxmediaimpl.platform.java.ID3MetadataParser.parse(Unknown Source)
    at com.sun.media.jfxmediaimpl.MetadataParserImpl.run(Unknown Source)
Exception in thread "main" java.lang.IllegalStateException: Toolkit not initialized
    at com.sun.javafx.application.PlatformImpl.runLater(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.runLater(Unknown Source)
    at javafx.application.Platform.runLater(Unknown Source)
    at javafx.scene.media.MediaPlayer.init(Unknown Source)
    at javafx.scene.media.MediaPlayer.<init>(Unknown Source)
    at core.SoundTest.sound(SoundTest.java:43)
    at core.SoundTest.main(SoundTest.java:13)

My sound file is located in the folder of my eclipse project where the class is in. It is a 3 minute long mp3 file located inside of the src and bin folders but not inside of the packages. (Im on windows).

How come this doesnt work? Why am I getting these errors.

Asperger
  • 3,064
  • 8
  • 52
  • 100
  • 1
    Just a note, the code you provided doesn't match the stack trace you provided. The stack trace says it's inside a method called `alarm()` but you provided `sound()`. – dcsohl May 31 '16 at 18:55
  • @dcsohl ah ya sorry. Thats not due to that though, ill change it real fast. I changed the method name. Anyways, I corrected it. – Asperger May 31 '16 at 18:56
  • Ok, next comment: Have you tried a debugger? Have you looked at what the actual `String` is that you're passing to `new Media()`? – dcsohl May 31 '16 at 19:00
  • @dcsohl I just did a System.out.print of media and got the following: javafx.scene.media.Media@6267c3bb – Asperger May 31 '16 at 19:01
  • I will try the eclipse debugger too now. Im not very trained in using it (coming from the web world) – Asperger May 31 '16 at 19:02
  • Not the media object itself; the `String` you are passing to its constructor. What is the value of `new File(path).toURI().toString()`? – dcsohl May 31 '16 at 19:02
  • @dcsohl now I put breakpoint on the line where I call this function. When debugging I get Thread.exit() source not found – Asperger May 31 '16 at 19:03
  • @dcsohl hey it seems the path is wrong? This is what I get: file:/C:/Users/RV/workspace/project/test.mp3 – Asperger May 31 '16 at 19:10
  • 1
    `java.lang.IllegalStateException: Toolkit not initialized` Tells you all you need. JavaFX platform is not initialized. – SnakeDoc May 31 '16 at 19:11
  • @SnakeDoc but I used javafx for grafics and stuff and never had this issue. I loaded jfxrt.jar and imported the right libraries – Asperger May 31 '16 at 19:13
  • "I loaded jfxrt.jar and imported the right libraries" did you also run the main method in javafx.application.Application to initialize Toolkit ? – Nigel Savage May 31 '16 at 19:18
  • @NigelSavage do you mean extending my class with Application and then add launch(); in the start method? Could you show an example? – Asperger May 31 '16 at 19:19
  • 1
    @Asperger Yes, simply having the library on the classpath doesn't instantiate the JavaFX platform. Here's an example of how to start the JavaFX platform: http://docs.oracle.com/javafx/2/get_started/hello_world.htm – SnakeDoc May 31 '16 at 19:21
  • @SnakeDoc thanks I really learned a lot now. I thought it only applies when working with graphics and stages. – Asperger May 31 '16 at 19:27

1 Answers1

7

The problem here is that MediaPlayer is meant to be use in a JavaFX application only so you need to convert your application as a JavaFX application if you want to be able to use it.

To convert your class into a JavaFX application you need:

  1. To make your class SoundTest extends javafx.application.Application
  2. And modify your main method as next

    public static void main(String[] args) { Application.launch(args); }

  3. You can then call the method sound in your implementation of start
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
  • I dont get it. I loaded javafx library by going to build path, configure path and I added jfxrt.jar and loaded the corresponding libraries via import – Asperger May 31 '16 at 19:12
  • it is not enough your class needs to extends javafx.application.Application and your main method will call Application.launch(args); – Nicolas Filotto May 31 '16 at 19:18
  • Application will initialize JavaFX for you – Nicolas Filotto May 31 '16 at 19:21
  • I will update my code – Asperger May 31 '16 at 19:21
  • Can you have a look at my question now? I updated my code. I extended Applicaton now and called the function inside the start method. – Asperger May 31 '16 at 19:23
  • Don't call launch() inside start, call launch inside main(args). Just look at the code [for a minimal JavaFX hello world application](https://docs.oracle.com/javase/8/javafx/get-started-tutorial/hello_world.htm) and base your implementation off of that. – jewelsea May 31 '16 at 19:25
  • @Asperger Do not call `launch()` from your start method! launch calls start (eventually), not the other way around. – VGR May 31 '16 at 19:25
  • My bad. Called it in my main : ) Accepted your answer – Asperger May 31 '16 at 19:26
  • You know, I thought this concept only applies to graphics such as when we want to add a primary stage and create buttons. I never thought it was an absolute requirement for launching javafx as a whole – Asperger May 31 '16 at 19:29
  • yes that is misleading indeed, sometimes I'm wondering if JavaFX is still really properly integrated into the JDK – Nicolas Filotto May 31 '16 at 19:32