e(fx)clipse 2.2.0 – Loading Media files from OSGi-Bundles – sponsored by 360T

As JavaFX is not only a UI-Toolkit but also provides a “fullblow” multimedia stack you can play sound files and videos without residing to other APIs/Libraries. Unfortunately you can not feed any URL into it.

Only the following protocols are supported

  • http:
  • file:
  • jar:file:

For most developers those protocols are enough but if you are running in an Equinox-OSGi-Environment and want to play a sound file packaged with in your bundle you are in trouble because the URL you’ll get is eg on Equinox bundleresource://.... so you can’t directly pass this URL to the JavaFX Media API because it will fail to resolve the resource.

Even if you know how to fix this and translate the URL to one of the above protocols your business code should never have a direct dependency on OSGi so you can not deal with this problem yourself but need a library to do this.

One of our JavaFX customers (360T) approach us a few days ago and so we implemented API giving you exactly this feature and you can/should always create Media instance like this:

public void playSound() {
  URL url = getClass().getResource("mysound.mp3");
  MediaLoader.createMedia( url )
    .map( MediaPlayer::new )
    .ifPresent( MediaPlayer::play );

If you are interested in other new features of 2.2.0 look at this overview page

This entry was posted in e(fx)clipse and tagged . Bookmark the permalink.

3 Responses to e(fx)clipse 2.2.0 – Loading Media files from OSGi-Bundles – sponsored by 360T

  1. Pingback: e(fx)clipse 2.2.0 – New features overview | Tomsondev Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.