16

I just started working with JavaFX. I know how the basics of it work. I tried to use the media and the mediaplayer classes to play a sound, called "sound.mp3". I am programming 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 thing 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
Neuron
  • 5,141
  • 5
  • 38
  • 59
user3010445
  • 215
  • 1
  • 2
  • 9
  • 1
    Based on `uri.getScheme() == null`, I assume you need to provide a URI for the sound file, instead of just a filename. – David Conrad Apr 21 '14 at 17:33
  • 1
    "The source must represent a valid URI and is immutable. Only HTTP, FILE, and JAR URLs are supported." http://docs.oracle.com/javafx/2/api/javafx/scene/media/Media.html#Media(java.lang.String) – David Conrad Apr 21 '14 at 17:34

6 Answers6

33

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 in your Project folder, alongside bin and src.

Any IDE will prompt you to add these as well:

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

Works like a charm.

anaik
  • 1,265
  • 11
  • 14
  • Thank you! I tried to use JOAL for playing sound, but it played a nasty popping sound at the end of every wav file, and also proved overly-complicated just for playing sounds. This solves my problem. – AvaLanCS Dec 30 '16 at 20:56
11
mApplause = new AudioClip(this.getClass().getResource("/sounds/applause.mp3").toExternalForm());

So this is what i used and it worked, i know it doesn't probably matter anymore but since it got in my google search while i was looking for something else i thought i would answer to it. :)

Meaning the toExternal Form it tells to form a url form of the path file.

Enes
  • 111
  • 1
  • 6
  • 1
    You should use AudioClip class only for playing short audio files. It stores y the raw, uncompressed audio data for the entire sound, which can be quite large for long audio clips. For more info: https://docs.oracle.com/javafx/2/api/javafx/scene/media/AudioClip.html – ryuujin Apr 02 '16 at 23:08
3

This is what I am currently using:

Media hit = new Media(new File(soundFilename).toURI().toString());
KisnardOnline
  • 653
  • 4
  • 16
  • 42
0

it depends on where is the audio file. (also it accepts only .wav) if the clip is outside the src folder you need to use

new Media("file:sound.wav")

if it's not it's ok how are you doing

Bonfra
  • 297
  • 1
  • 11
0

The other answers here didn't work for me, but this did the job:

Media sound = new Media(getClass().getResource("sounds/sound.mp3").toExternalForm());
MediaPlayer mediaPlayer = new MediaPlayer(sound);
mediaPlayer.play();
ravi
  • 75
  • 6
0

I had the same problem. In my case, it was about accesses given to javaFX. Assuming that you use IntelliJ IDEA, go to Run -> Edit Configurations, select "Modify Options", click on "Add VM Options", and paste this: --module-path /PATH/TO/YOUR/JAVAFX/LIB --add-modules javafx.controls,javafx.fxml,javafx.base,javafx.media (Replace /PATH/TO/YOUR/JAVAFX/LIB with directory of the "lib" folder in javafx installation directory on your computer. This is similar to what is said in this official tutorial:Create-a-javafx-project-with-IntelliJ ; But modules javafx.media and javafx.base are added.

Note that based on my searches and experience, javaFX 11 cannot play .mp3 files (unlike .wav files). You may want to consider updating javaFX to 16 to play .mp3 files.

Sina
  • 1
  • 1
  • I don't know where you found this information, but it is incorrect. JavaFX 11 [can play MP3 files](https://openjfx.io/javadoc/11/javafx.media/javafx/scene/media/package-summary.html). – Tech Expert Wizard Nov 13 '21 at 01:48