Javatpoint Logo
Javatpoint Logo

Playing Audio

We can load the audio files with extensions like .mp3,.wav and .aifff by using JavaFX Media API. We can also play the audio in HTTP live streaming format. It is the new feature introduced in JavaFX 8 which is also known as HLS.

Playing audio files in JavaFX is simple. For this purpose, we need to instantiate class by passing the audio file path in its constructor. The steps required to be followed in order to play audio files are described below.

  1. Instantiate the class by passing the location of the audio file in its constructor. Use the following line of code for this purpose.
  2. Pass the Media class object to the new instance of object.
  3. Invoke the MediaPlayer object's play() method when onReady event is triggered.

The Media File can be located on a web server or on the local file system. SetAutoPlay() method is the short-cut for setting the setOnReady() event handler with the lambda expression to handle the event.


In the following example, the audio file located at "/home/javatpoint/Downloads/test.mp3" in our computer is played upon executing this application.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA