How to create a Simple JavaFX Music Player

In this chapter, we will make a simple JavaFX music player to demonstrate the use of different JavaFX UI controls. It uses a file chooser, button and label with javafx.scene.media.MediaPlayer class. It displays the mp3 file location in label and has buttons to play, pause and stop the music. You can take this code as an example to develop better music players.

import java.io.File;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class MusicPlayer extends Application {

    private Stage stage;
    private String audioFile = null;
    private MediaPlayer player;
    private Boolean fileinuse = false;
    Label musicLabel = new Label("No File");
    
    public void chooseFile() throws MalformedURLException{
            FileChooser chooser = new FileChooser();
            FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("select your media(*.mp3)", "*.mp3");
            chooser.getExtensionFilters().add(filter);
            File file = chooser.showOpenDialog(this.stage);
            if ( file !=null){
                musicLabel.setText(file.getPath());
                this.audioFile = file.toURI().toURL().toString();
            }else{
                musicLabel.setText("Invalid Filename");
                this.audioFile = null;
            }
            fileinuse = false;
            player.stop();
    }
    
    public void playFile(){
            if(fileinuse == true){
                player.play();
            }else{
                Media pick = new Media(this.audioFile);
                player = new MediaPlayer(pick);
                player.play();   
            }
            fileinuse = true;
    }
    
    public void pauseFile(){
            player.pause();
    }
    
    public void stopFile(){
            player.stop();
    }

    @Override
    public void start(Stage primaryStage) {
        
        musicLabel.setWrapText(true);
        this.stage = primaryStage;
        StackPane root = new StackPane();
        Button chooseButton = new Button("Choose MP3 File");
        Button playButton = new Button("Play");
        Button pauseButton = new Button("Pause");
        Button stopButton = new Button("Stop");
        HBox hbox1 = new HBox(20, chooseButton, playButton, pauseButton, stopButton);
        HBox hbox2 = new HBox(musicLabel);
        VBox vbox = new VBox(hbox1,hbox2);
        
        chooseButton.setOnAction((ActionEvent e) -> {
            try {
                this.chooseFile();
            } catch (MalformedURLException ex) {
                Logger.getLogger(MusicPlayer.class.getName()).log(Level.SEVERE, null, ex);
            }
        });
        
        playButton.setOnAction((ActionEvent e) -> {
            this.playFile();
        });
        
        pauseButton.setOnAction((ActionEvent e) -> {
            this.pauseFile();
        });
        
        stopButton.setOnAction((ActionEvent e) -> {
            this.stopFile();
        });

        root.getChildren().add(vbox);
        Scene scene = new Scene(root);
        primaryStage.setTitle("Music Player");
        primaryStage.setScene(scene);
        primaryStage.show();
  }
    
  public static void main(String[] args) {
    launch(args);
  }
  
}

Leave a Reply

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