How to play sound in JavaFX?


Question

I'm experimenting with JavaFX making a small game.

I want to add sound. How?

I tried MediaPlayer with media defined with relative source attribute like:

attribute media = Media{
    source: "{__FILE__}/sound/hormpipe.mp3"        
}
attribute player = MediaPlayer{
    autoPlay:true
    media:media
}

It doesn't play. I get

FX Media Object caught Exception com.sun.media.jmc.MediaUnavailableException: Media unavailable: file: ... Sound.class/sound/hormpipe.mp3

1
0
5/31/2015 6:55:25 AM

var player = javafx.scene.media.MediaPlayer { repeatCount: javafx.scene.media.MediaPlayer.REPEAT_FOREVER media: Media { source: "{__DIR__}clip.wav" }; }; player.play();

You have to incluye the audio file in the build/compiled directory so Netbeans can pack it into the jar file.

1
12/15/2008 12:44:07 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon