title: Playing audio using JavaFX MediaPlayer in a normal Java application?
body: <p>I need to be able to play Audio files (MP3 / Wav) in a normal Java project. I'd prefer using the new JavaFX MediaPlayer rather than JMF. I wrote some code to test this:</p>
  
  <pre><code>public void play()
  {
      URL thing = getClass().getResource("mysound.wav");
      Media audioFile = new Media( thing.toString() );     
      try
      {                                       
          MediaPlayer player = new MediaPlayer(audioFile);
          player.play();
      }
      catch (Exception e)
      {
          System.out.println( e.getMessage() );
          System.exit(0);
      }        
  }
  </code></pre>
  
  <p>When I run this, I get the exception: Toolkit not initialized</p>
  
  <p>I get that this has something to do with the JavaFX thread. My question is, how can I solve this? Do I need to create a JavaFX Panel just to play some audio files in the background of my normal app, or is there any other way?</p>
  
  <p>Edit: Stacktrace:</p>
  
  <pre><code>java.lang.IllegalStateException: Toolkit not initialized
      at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:121)
      at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:116)
      at javafx.application.Platform.runLater(Platform.java:52)
      at javafx.scene.media.MediaPlayer.init(MediaPlayer.java:445)
      at javafx.scene.media.MediaPlayer.&lt;init&gt;(MediaPlayer.java:360)
      at javaapplication6.JavaApplication6.play(JavaApplication6.java:23)
      at javaapplication6.JavaApplication6.main(JavaApplication6.java:14)
  </code></pre>