Audio Play

The section demonstrate how to load audio and play it in JavaFX. It's very fast and easy to load an audio using JavaFX. Lets observe a basic example on loading an audio. The audio can be load from the hard-drive or from the internet.


Example


 



Click here to download the example


We create a media player outside the stage. Then we set the song to auto play whenever the program is run. Now, let see a more interesting example. The following example will have two buttons. The music will only start when the user clicks the start button, and the music will stop once the user clicks the stop button.


Example


Click here to download the example


The above example, we used the play() and stop(). It allows us to play the music or stop the music whenever the user prefer. Notice for this example we didn't include the autoPlay in the MediaPlayer definition because we are allowing the user to choose when the music should start and stop.

 

  <JavaFX><Home>