Inserindo Audio (MP3)

Com JavaFX e Maven

Sem imagem

Arquivo: App.java
package minhaappjavafx.MinhaAppJavaFX;

import javafx.application.Application;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
import java.net.URL;

public class App extends Application {
    @Override
    public void start(Stage primaryStage) {
        // 1. Locate the file in the resources folder
        URL resource = getClass().getResource("/music/song.mp3");
        
        if (resource == null) {
            System.out.println("Error: MP3 file not found in resources!");
            return;
        }

        // 2. Convert URL to string format and instantiate Media
        Media media = new Media(resource.toExternalForm());
       
        // 3. Create the player and play the audio
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        mediaPlayer.play();
    }

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