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 javafx.scene.media.Media 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 javafx.scene.media.Media 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 javafx.scene.media.MediaPlayer 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.

Example

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






Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA