Kensoft PH
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
No Result
View All Result
Home Java

JavaFX Music Player Tutorial with JavaFX Media Player

October 5, 2023
in Java
Reading Time: 6 mins read
0
JavaFX Music Player
586
VIEWS
Share on FacebookShare on TwitterShare via Email

In the field of software development, creating a cool and user-engaging Music Player application is a popular use case that developers often come across. This tutorial aims to guide you in developing a basic and simple music player using JavaFX platform. Specifically, it is a basic and fundamental tutorial on how to implement JavaFX Music Player with JavaFX Media Player.

Contents

Toggle
  • A Sneak Peek of JavaFX Media Player
  • Dive-In: Developing a basic and simple JavaFX Music Player
    • Step1: Equip Yourself: Setting up JavaFX
    • Step 2: Build your Stage: The Interface
    • Step 3: The Backbone: Integrating JavaFX Media Player
      • pom.xml
      • module-info.java
    • Step 4: Making it Pop: Play, Pause, Next, Previous Functionality
      • Full source code of my basic and simple music player
        • FXML File
        • FXML Controller Java Class
        • Main Class
    • Step 5: Finishing and Testing
  • The Bottom-line
  • Youtube Video

A Sneak Peek of JavaFX Media Player

JavaFX Media Player is a component, specifically class javafx.scene.media.MediaPlayer, designed to take care of media playback in a straightforward manner. It can play a variety of media types but in this walk-through, our focus will be on utilizing the JavaFX Media Player for creating a JavaFX Music Player that can play your favorite tracks.

Dive-In: Developing a basic and simple JavaFX Music Player

So, are you stoked to build and perhaps, get your tunes playing in no time? Let’s dig in!

Screenshot 1

Step1: Equip Yourself: Setting up JavaFX

First thing first, ensure you’re geared up! Have the JavaFX SDK set up on your development environment. Besides, an IDE with support for JavaFX projects, like the IntelliJ IDEA, would be beneficial.

Step 2: Build your Stage: The Interface

Designing the JavaFX Music Player begins with setting up your interface using SceneBuilder. Start off by making a new JavaFX project and open the FXML file in SceneBuilder to create your awesome user interface. Key elements that your interface should contain include functionalities like “Play”, “Pause”, “Next”, “Previous” buttons, perhaps a “Volume” control, and other elements you’d love on a music player.

Step 3: The Backbone: Integrating JavaFX Media Player

After setting up our interface, it’s time to work on the code to bring life into the application. Inside the class that stands as our controller class of our FXML file, we initialize the Media and MediaPlayer objects

But before that, if you’re using the Maven build, you can download another module using the dependency at the pom.xml file and require it at the module-info.java to access the Media Classes.

pom.xml

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-media</artifactId>
            <version>17.0.6</version>
        </dependency>

module-info.java

module com.kensoftph.mediaplayer {
    requires javafx.controls;
    requires javafx.fxml;
    requires javafx.media;


    opens com.kensoftph.mediaplayer to javafx.fxml;
    exports com.kensoftph.mediaplayer;
}
Media media = new Media(new File("path_to_your_music_file.mp3").toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(media);

Remember to import javafx.scene.media.Media and javafx.scene.media.MediaPlayer.

Step 4: Making it Pop: Play, Pause, Next, Previous Functionality

How do we get the buttons doing what they’re supposed to? That’s simple! We implement these functions by adding event handlers for the buttons created in your FXML file.

For example, to make your play and pause functionality work, you could use:

playButton.setOnAction(e -> mediaPlayer.play());

pauseButton.setOnAction(e -> mediaPlayer.pause());

Similarly, you can build functionalities for “Next”, “Previous”, and “Volume” control. It’s as simple as loading the right file into your MediaPlayer object or tweaking the volume property.

Full source code of my basic and simple music player

In this code, I will provide you the source code that was developed with SceneBuilder. You can copy these code here and implement this to your own project.

FXML File
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>


<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="342.0" prefWidth="455.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.kensoftph.mediaplayer.MusicPlayer">
   <center>
      <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
         <children>
            <Label text="Simple Music Player">
               <font>
                  <Font size="40.0" />
               </font>
            </Label>
            <Label fx:id="chooseMusic" onMouseClicked="#chooseMusic" text="Choose your music">
               <font>
                  <Font size="15.0" />
               </font>
            </Label>
            <HBox alignment="CENTER" prefHeight="56.0" prefWidth="455.0" spacing="10.0">
               <children>
                  <Button mnemonicParsing="false" onMouseClicked="#play" text="Play">
                     <font>
                        <Font size="15.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onMouseClicked="#pause" text="Pause">
                     <font>
                        <Font size="15.0" />
                     </font>
                  </Button>
                  <Button mnemonicParsing="false" onMouseClicked="#stop" text="Stop">
                     <font>
                        <Font size="15.0" />
                     </font>
                  </Button>
               </children>
            </HBox>
         </children>
      </VBox>
   </center>
</BorderPane>
FXML Controller Java Class
package com.kensoftph.mediaplayer;

import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.FileChooser;

import java.io.File;

public class MusicPlayer {

    @FXML
    private Label chooseMusic;

    private MediaPlayer mediaPlayer;

    @FXML
    void chooseMusic(MouseEvent event) {
        FileChooser chooser = new FileChooser();
        chooser.setTitle("Select your music");
        File file = chooser.showOpenDialog(null);
        if(file != null){
            String selectedFile = file.toURI().toString();
            Media media = new Media(selectedFile);
            mediaPlayer = new MediaPlayer(media);
            mediaPlayer.setOnReady(() -> chooseMusic.setText(file.getName()));
        }
    }

    @FXML
    void pause(MouseEvent event) {
        mediaPlayer.pause();
    }

    @FXML
    void play(MouseEvent event) {
        mediaPlayer.play();
    }

    @FXML
    void stop(MouseEvent event) {
        mediaPlayer.stop();
    }

}
Main Class
package com.kensoftph.mediaplayer;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class Main extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("music-player.fxml"));
        Scene scene = new Scene(fxmlLoader.load());
        stage.setTitle("JavaFX Music Player");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}
JavaFX Media Player

Step 5: Finishing and Testing

Now that all functionalities are implemented, go ahead and run your JavaFX application. What do you see? An interactive JavaFX Music Player by you, for you!

The Bottom-line

Truly, you can now share in the pride of having the foundational knowledge to implement a JavaFX Music Player. JavaFX makes it easy to do so much, creating music players with smooth user interfaces and easy media file handling skillfully. Always remember to write your code in a form other developers can understand and maintain easily. This way, your JavaFX Music Player can be efficient, scalable and a joy to interact with. Until next time, happy coding!

Youtube Video

YouTube video
Previous Post

How to Play Video in JavaFX MediaPlayer from a File

Next Post

JavaFX Drag and Drop Tutorial | Perfect For Beginners

KENSOFT

KENSOFT

What’s up! Kent is my name. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java

Related tutorials

How to Use the JavaFX Pie Chart 100% For Beginners
Java

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
205
How to Connect to an API Using JavaFX
Java

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
215
JavaFX SQLite Database CRUD Tutorial
Java

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
587
Next Post
JavaFX Drag and Drop

JavaFX Drag and Drop Tutorial | Perfect For Beginners

How to take a screenshot on PC using Kenshot

How to Take a Screenshot on PC Using Kenshot: A Full Guide

JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tools

Multi-platform installer builder

Java profiler

  • Trending
  • Comments
  • Latest
MySQL database using XAMPP

How to connect Java to MySQL database using Xampp server | 100% best for beginners

October 27, 2020 - Updated on January 23, 2023
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

April 11, 2021 - Updated on July 3, 2022
JavaFX 17

How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

November 15, 2021 - Updated on December 13, 2021
hide and show password in jPasswordField

JPasswordField in Java Hide or Show Password | 100% best for beginners

April 2, 2021 - Updated on September 21, 2022
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

3DES in Java and AES in Java

How to use AES and 3DES in Java | 100% best for beginners

JavaFX Splash Screen

How to create JavaFX Splash Screen | 100% best for beginners

set up JavaFX and Scene Builder

How to set up JavaFX and Scene Builder in NetBeans IDE | 100% best for beginners

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
How to take a screenshot on PC using Kenshot

How to Take a Screenshot on PC Using Kenshot: A Full Guide

January 18, 2024 - Updated on October 6, 2024

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

Popular Tutorials

  • MySQL database using XAMPP

    How to connect Java to MySQL database using Xampp server | 100% best for beginners

    0 shares
    Share 0 Tweet 0
  • Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

    0 shares
    Share 0 Tweet 0
  • How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

    0 shares
    Share 0 Tweet 0
Facebook Instagram Youtube Github LinkedIn Discord
Kensoft PH

What’s up! I'm Kent. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java, which I use to create computer applications. In a company, I created applications and a website.

Categories

Website

Check the status

Privacy Policy

Terms and Condition

Sitemap

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

© 2024 Made With Love By KENSOFT PH

No Result
View All Result
  • Download
    • Kenshot
  • Contact
  • About
  • Java Quiz

© 2024 Made With Love By KENSOFT PH

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.