Play media on Shoes4 with JavaFX MediaPlayer

For Shoes4 the video method is not yet available so if you want to play an mp3 file for example on Shoes4 you can use the MediaPlayer class from JavaFX.
There is only one trick that you’re not allowed to miss and the trick is that you have to create a new instance of JFXPanel because the MediaPlayer is depending on the JavaFX interface. Otherwise you will experience an java.lang.IllegalStateException: Toolkit not initialized exception.
For example to play file.mp3 you can run the following code:

require 'shoes'

java_import javafx.scene.media.Media
java_import javafx.scene.media.MediaPlayer
java_import javafx.embed.swing.JFXPanel

Shoes.app do
  JFXPanel.new

  media = Media.new("file:///Users/andrei/Downloads/a.mp3")
  @player = MediaPlayer.new(media)
  @player.play
end