When i truing to play sound in mp3 format, code throwes me error: "Exception in thread "main" java.lang.IllegalStateException: Toolkit not initialized".
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import java.io.*;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
public class Main {
public static void main(String[] args) {
new Main();
}
int id = 467339;
public Main(){
try {
downloadFile(new URL("https://www.newgrounds.com/audio/download/"+id),id+".mp3");
Media media = new Media(new File(id+".mp3").toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.play();
} catch (IOException e) {
e.printStackTrace();
}
}
public void downloadFile(URL url, String outputFileName) throws IOException {
try (InputStream in = url.openStream();
ReadableByteChannel rbc = Channels.newChannel(in);
FileOutputStream fos = new FileOutputStream(outputFileName)) {
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
}
}