mardi 19 juin 2012

MediaPlayer keeping quiet

According to the javafx media tutorial, playing a sound is simple. JavaFX provides a MediaPlayer to easily play any media source. You just need to define your Media by it’s URI, so this should looks like:
This seems very easy, but if you tries this, you might here nothing!
Then you are certainly in one of the two following cases:
  • Your sound resource is packaged within your jar. This is not supported for the moment, see javafx faq
  • You are trying to play a short sound sample. The first time I tried to play a 2s sound sample, my speakers kept desperately quiet. So what happened? A look at the Improve Media Performance tutorial put me on the way to the solution: according to this article, “for faster playback of a streaming media that is progressively downloaded, it helps to initialize the engine first by using the same media behind the scenes”. So, when you call the play function of the MediaPlayer, that playback engines “crunches” the first seconds of the sound sample during it’s initialization. For a long sound, you just almost don’t notice it, but a short sound sample might totally vanish. Unfortunately the MediaPlayer does not provides an init method, and the only way you can do it for the moment is to set your player to mute, play your media once and then rewind and unmute. It’s quiet ugly, but it’s the “official” solution proposed in the previous article. If you have a better solution, I’d be happy to hear it. Or just hope that javafx 1.3 will add an init method to the MediaPlayer.

Aucun commentaire:

Publier un commentaire