Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java audio is not loading. toURI not working? [duplicate]

Tags:

java

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.

like image 832
Asperger Avatar asked Jul 19 '26 14:07

Asperger


1 Answers

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
like image 131
Nicolas Filotto Avatar answered Jul 21 '26 03:07

Nicolas Filotto