Hi,
The code snippet below is a simple program to play an audio song file. The MP3 file is located in the same folder as this code. It compiles without any errors. But when I run the code it gives me two "java.lang.IllegalStateException: Toolkit not initialized" exceptions. Below is full output of excepetion error:
Exception in thread "Thread-0" java.lang.IllegalStateException: Toolkit not initialized
at com.sun.javafx.application.PlatformImpl.runLater(U nknown Source)
at com.sun.javafx.application.PlatformImpl.runLater(U nknown Source)
at javafx.application.Platform.runLater(Unknown Source)
at javafx.scene.media.Media$_MetadataListener.onMetad ata(Unknown Source)
at com.sun.media.jfxmediaimpl.MetadataParserImpl.done (Unknown Source)
at com.sun.media.jfxmediaimpl.platform.java.ID3Metada taParser.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(U nknown Source)
at com.sun.javafx.application.PlatformImpl.runLater(U nknown Source)
at javafx.application.Platform.runLater(Unknown Source)
at javafx.scene.media.MediaPlayer.init(Unknown Source)
at javafx.scene.media.MediaPlayer.<init>(Unknown Source)
//import necessary packages import javafx.scene.media.Media; import javafx.scene.media.MediaPlayer; import java.io.*; public class PlaySong { public static void main(String args[]) { String song = new File("song.mp3").toURI().toString(); Media audio =null; audio = new Media(song); MediaPlayer mediaPlayer = new MediaPlayer(audio); mediaPlayer.play(); } }
Can anyone identify the problem? Iv'e been looking everywhere for solutions, no luck.
Thanks and much appreciated.