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 Polyline, JavaFX Arc, JavaFX Circle, Intersection operation, JavaFX Rotate Transition, Coding Ninjas Studio Interview Bundle, Coding Ninjas Studio Interview Experiences, Coding Ninjas Courses, Coding Ninjas Studio Contests, and Coding Ninjas Studio Test Series. Do upvote our blog to help other ninjas grow.
Happy Coding!