简体   繁体   中英

How to play sounds with JavaFX

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

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.

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.

这是我目前正在使用的:

Media hit = new Media(new File(soundFilename).toURI().toString());

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

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();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM