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.