title: Do I need multiple MediaPlayers for multiple media files?
body: <p>I am developing a music library application with JavaFX and I am having some issues with how I am supposed to correctly use the MediaPlayer class.</p>
  
  <p>This is my current method of setting and playing audio files:</p>
  
  <pre><code>private static MediaPlayer musicPlayer;
  
  public static void setMedia(String path) {
      try {
          musicPlayer = new MediaPlayer(new Media(new File(path).toURI().toASCIIString()));
          musicPlayer.play();
      } catch (Exception e) {
          System.out.println("invald file");
          System.out.println(e.getMessage());
      }
  }
  </code></pre>
  
  <p>Say for example, I have two audio files...</p>
  
  <pre><code>file1.mp3
  file2.mp3
  </code></pre>
  
  <p>...And two buttons that send one of these two files to the above method:</p>
  
  <pre><code>button1.setOnAction(event -&gt; {
      setMedia("file1.mp3");
  });
  
  button2.setOnAction(event -&gt; {
      setMedia("file2.mp3");
  });
  </code></pre>
  
  <p>Normally it would make sense that both audio files would play over each other because I am creating a new MediaPlayer object each time I call the method, however, this object is static. Maybe I am misinterpreting how static objects work, but shouldn't it mean that there can only ever be one instance of this object? And in turn, wouldn't that mean that whenever the setMedia method is called, the MediaPlayer object's media would be overwritten?</p>
  
  <p>I have not found any way to "set" or "overwrite" media for a MediaPlayer object without creating a new instance of it. It does not appear to have a method to do so.
  This leads me to believe that I must have an array of MediaPlayer objects for each audio file. Is this correct? Or am I simply missing something here?</p>