How to play sounds using JavaFX

I just started working with JavaFX. I know the basics of his work. I tried using the media and media player classes to play sound called "sound.mp3". I program in eclipse and I have the sound file in the src folder, the same folder as "(default package)". Here is my code:

import javafx.scene.media.*; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class Main extends Application{ public static void main(String[] args){ launch(args); } @Override public void start(Stage primaryStage){ primaryStage.setTitle("Hello World!"); String ssound = "sound.mp3"; Media sound = new Media(ssound); MediaPlayer mediaPlayer = new MediaPlayer(sound); mediaPlayer.play(); StackPane root = new StackPane(); primaryStage.setScene(new Scene(root, 800, 450)); primaryStage.show(); } } 

Please tell me what I'm doing wrong.

Here is the error message from the console:

 Exception in Application start method Exception in thread "main" java.lang.RuntimeException: Exception in Application start method at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source) at com.sun.javafx.application.LauncherImpl.access$000(Unknown Source) at com.sun.javafx.application.LauncherImpl$1.run(Unknown Source) at java.lang.Thread.run(Thread.java:724) Caused by: java.lang.IllegalArgumentException: uri.getScheme() == null! at com.sun.media.jfxmedia.locator.Locator.<init>(Unknown Source) at javafx.scene.media.Media.<init>(Unknown Source) at Main.start(Main.java:16) at com.sun.javafx.application.LauncherImpl$5.run(Unknown Source) at com.sun.javafx.application.PlatformImpl$5.run(Unknown Source) at com.sun.javafx.application.PlatformImpl$4$1.run(Unknown Source) at com.sun.javafx.application.PlatformImpl$4$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl$4.run(Unknown Source) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source) at com.sun.glass.ui.win.WinApplication$3$1.run(Unknown Source) ... 1 more 
+7
java javafx audio media
source share
3 answers

Just a working compilation of what the other answers say:

 String musicFile = "StayTheNight.mp3"; // For example Media sound = new Media(new File(musicFile).toURI().toString()); MediaPlayer mediaPlayer = new MediaPlayer(sound); mediaPlayer.play(); 

Add the music file to the Project folder along with bin and src .

Any IDE will offer you to add them as well:

 import javafx.scene.media.Media; import javafx.scene.media.MediaPlayer; import java.io.File; 

It works like a charm.

+9
source share
 mApplause = new AudioClip(this.getClass().getResource("/sounds/applause.mp3").toExternalForm()); 

So, this is what I used and it worked, I know that it probably doesn't matter, but since it got into my google search query, when I searched for something else, I thought I would answer him. :)

The toExternal Form value that it reports to form the path file URL form.

+3
source share

This is what I am using now:

 Media hit = new Media(new File(soundFilename).toURI().toString()); 
+2
source share

All Articles