title: How to play same audio clip multiple times using JavaFX MediaPlayer
body: <p>I want to implement a simple task:
  when a button is clicked, I want to play a sound.
  I have 3 separate buttons that, when clicked, call the same function.
  In this function, I play the audio clip.
  The tricky part is: sometimes the buttons work just fine, sometimes not at all, which is super weird to me since they all call the same function.</p>
  
  <p>I've browsed other javafx-audio-related questions but they are all about the audio not playing at all, whereas in my case it seems to play only sometimes?</p>
  
  <p>FXML snippet:</p>
  
  <pre><code>&lt;ImageView fx:id="bomb1" fitHeight="32.0" fitWidth="32.0" 
             onMouseClicked="#useTheBomb"
             pickOnBounds="true" preserveRatio="true"&gt;
      &lt;Image url="@bombs/bomb1.png"/&gt;
      &lt;cursor&gt;
          &lt;Cursor fx:constant="HAND"/&gt;
      &lt;/cursor&gt;
  &lt;/ImageView&gt;
  &lt;ImageView fx:id="bomb2" fitHeight="32.0" fitWidth="32.0" 
             onMouseClicked="#useTheBomb"
             pickOnBounds="true" preserveRatio="true"&gt;
      &lt;Image url="@bombs/bomb2.png"/&gt;
      &lt;cursor&gt;
          &lt;Cursor fx:constant="HAND"/&gt;
      &lt;/cursor&gt;
  &lt;/ImageView&gt;
  &lt;ImageView fx:id="bomb4" fitHeight="32.0" fitWidth="32.0" 
             onMouseClicked="#useTheBomb"
             pickOnBounds="true" preserveRatio="true"&gt;
      &lt;Image url="@bombs/bomb4.png"/&gt;
      &lt;cursor&gt;
          &lt;Cursor fx:constant="HAND"/&gt;
      &lt;/cursor&gt;
  &lt;/ImageView&gt;
  </code></pre>
  
  <p>This code is placed in my FXML Controller class</p>
  
  <pre><code>private val explosion = App::class.java.getResource("/bombs/explosion.mp3").toString()
  private fun explode() = MediaPlayer(Media(explosion)).play()
  </code></pre>
  
  <p>the function that is called by the buttons:</p>
  
  <pre><code>fun useTheBomb() {
          explode()
  
          /* rest of the function does not concern audio at all, 
           * but I include it in case there's maybe some voodoo magic 
           * underneath that I don't know about
           * bombArray: Array&lt;ImageView&gt;
           */        
          with(bombArray[brikks.bombs++]) {
              image = exploded
              onMouseClicked = null
              cursor = Cursor.DEFAULT
          }
  }
  </code></pre>
  
  <p>I get no errors when clicking the buttons</p>
  
  <p>I also modyfied my program to allow the buttons to be clicked multiple times, and out of 30 tries the audio failed to play 6 times randomly. I couldn't see a pattern.</p>