Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 658
user3010445 Avatar asked Apr 21 '14 17:04

user3010445


People also ask

How do you run a song in Java?

Following steps are to be followed to play a clip object. getAudioInputStream(File file). AudioInputStream converts an audio file into stream. Get a clip reference object from AudioSystem. Stream an audio input stream from which audio data will be read into the clip by using open() method of Clip interface.

What is audio clip in Java?

public final class AudioClip extends Object. An AudioClip represents a segment of audio that can be played with minimal latency. Clips are loaded similarly to Media objects but have different behavior, for example, a Media cannot play itself.


2 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 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.

like image 85
anaik Avatar answered Oct 23 '22 11:10

anaik


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.

like image 35
Enes Avatar answered Oct 23 '22 11:10

Enes