tags:

views:

2161

answers:

4

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  A: 

Just a guess, but is that file "hornpipe.mp3" and not "hormpipe.mp3" (with an m)?

GuyWithDogs
Nope file name is ok.Anyone played sound in JavaFX?
Chobicus
+1  A: 

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.

A: 

Just a guess, but I think your {__FILE__} will expand to the name of your file. Try replacing it with {__DIR__}.

lindelof
A: 

Also note that {__DIR__} includes the trailing /, so try this instead:

attribute media = Media{
source: "{__DIR__}sound/hormpipe.mp3"}

EDIT: I did some digging, and apparently, the source of a Media object has to be either a remote URL, or an absolute file path, since media files aren't allowed in JARs (something I hope gets changed with future releases, since I really like JavaFX and want to be able to make desktop apps with it). See: JavaFX FAQs.

MKA