JavaFX load sound

--

Add javafx.media

Add the following dependencies in pom.xml (inside root)

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

After adding these lines, refresh it:

Right click → Maven → Reload projects

Add the following line in module-info.java (inside /src/main/java/)

requires javafx.media;
String musicFile = "C:\\Users\\Admin\\IdeaProjects\\demo3\\demo1\\src\\main\\resources\\phone.mp3";     // For example
Media sound = new Media(new File(musicFile).toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(sound);
mediaPlayer.play();

--

--