Table of contents
1.
Introduction
2.
How to Play Audio 
3.
Example of Playing Audio using JavaFX
4.
Frequently Asked Questions
4.1.
For what purpose is JavaFX used?
4.2.
JavaFX is open source or not?
4.3.
Support for audio and video codecs is provided by JavaFX?
4.4.
Explain the components of JavaFX?
4.5.
Name the different transformations in JavaFX?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Playing Audio Using JavaFX

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this article, you will learn how to play the audio using the JavaFX class and its methods.

JavaFX creates and disseminates desktop and rich web applications that function on various platforms. Desktop computers, web browsers running Microsoft Windows, Linux, and macOS, as well as mobile devices running iOS and Android, are all compatible with JavaFX.

How to Play Audio 

Using JavaFX Media API, we can load audio files with extensions like .mp3.wav, and .aifff. Additionally, the audio can be played in HTTP live streaming format. It is a brand-new function that was added to JavaFX 8 and is also referred to as HLS.

JavaFX makes it easy to play audio files. The audio file path must be passed to the constructor of the javafx.scene.media.Media class to achieve this. Below are the instructions that must be performed to play audio files.

Step1: Pass the path of the audio file in the constructor of javafx.scene.media.Media class

Media ourMedia = new Media("http://path/music_name.mp3");  


Step2: Pass the Media class object to the constructor of javafx.scene.media.MediaPlayer class to instantiate its object.

Mediaplayer ourMediaPlayer = new MediaPlayer(ourMedia); 


Step3: Call the MediaPlayer object's play() method when the onReady event is triggered.

MediaPlayer.setAutoPlay(true);  

Example of Playing Audio using JavaFX

In this section, we will discuss an example of playing audio using JavaFX. Here, we will simply program a small code on whose execution sound will be played, which will be provided by us. In the example that follows, the audio file "/path/music_name.mp3" on our machine is played when this program is launched. 

Code:

package application;
import java.io.File;
 
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
public class JavaFX_Media Example extends Application
{
	@Override
	public void start (Stage primaryStage) throws Exception {
		// TODO This is Auto-generated method stub
		//Initialising path of the media file, you can replace this with your file path 
		String musicPath = "D:\JavaFX\demo_music.mp3";

		//Initialising the Media class
		Media ourMedia = new Media(new File(musicPath).toURI().toString());

		//Initialising MediaPlayer class 
		MediaPlayer ourMediaPlayer = new MediaPlayer(ourMedia);

		//After setting this property to true, the audio will be played 
		mediaPlayer.setAutoPlay(true);
		primaryStage.setTitle("Playing Audio");
		primaryStage.show();
	}
	public static void main(String[] args) {
		launch(args);
	}
}

Frequently Asked Questions

For what purpose is JavaFX used?

Using JavaFX, a set of graphics and media packages, programmers can design, build, test, debug, and deploy sophisticated client applications that function dependably across numerous platforms.

 

JavaFX is open source or not?

The OpenJFX open source project currently has the JavaFX UI Controls source code; other JavaFX components are anticipated to be added over time. Like other OpenJDK projects, the source is available under the GPL v2 with the Classpath Exception license.

 

Support for audio and video codecs is provided by JavaFX?

A single set of APIs that JavaFX offers make it simple to integrate media playback into every JavaFX application. The following media formats are currently supported:

Audio: MP3, AIFF with uncompressed PCM, WAV with uncompressed PCM, and MPEG-4 multimedia container with Advanced Audio Coding are all audio formats (AAC).

Video: H.264/AVC (Advanced Video Coding) video compression is used in FLV, a multimedia container for MPEG-4 data.

 

Explain the components of JavaFX?

There are three components in the JavaFX application:

  • Stage: This includes every JavaFX object.
  • Scene: This depicts the actual contents of the JavaFX application.
  • Nodes: It is the JavaFX.scene package that illustrates a node.

 

Name the different transformations in JavaFX?

Different transformations of JavaFX are

  • Translation
  • Scaling up and down
  • Rotation
  • shearing

Conclusion

In this article, we have extensively discussed how to play audio using JavaFX by using the classes JavaFX offers.

We hope this blog has helped you enhance your knowledge regarding playing audio using JavaFX. Check out the awesome content on the Coding Ninjas Website, JavaFX PolylineJavaFX Arc, JavaFX CircleIntersection operationJavaFX Rotate TransitionCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test SeriesDo upvote our blog to help other ninjas grow. 

Happy Coding!

Live masterclass