0

In Netbeans 8.0 I have a project with multiple modules. I want one module to open a file chooser for audio files that the app will play using javax.sound.sampled library. I use AudioSystem.getAudioFileTypes() to get the file types that can be used and see that there are only 3 available ('wav', 'au' and 'aif') but I'd also like to be able to deal with mp3, ogg and flac.

So I downloaded the SPIs for the codecs I need. All the documentation tells me that I need only put the jars into the classpath and the codecs will become available to Java's sound library. In my project I want more than one module to be able to use these libraries, so I created module wrappers for them. So, for example, I have in my project one module called jflac-1.3-jdk which wraps two jars: jflac-1.3-jdk1.4.jar and jflac-1.3.jar then I make the module that is going to open the file chooser depend on these modules that wrap jars.

Still there are only the same 3 file types available. What am I missing?

Here is the code in the top component of the module that depends on the wrapper modules:

private static String[] audioFileExtensions;
static
{
    AudioFileFormat.Type[] formatTypes = AudioSystem.getAudioFileTypes();
    audioFileExtensions = new String[formatTypes.length];
    for (int i = 0; i < formatTypes.length; i++) {
        audioFileExtensions[i] = formatTypes[i].getExtension();
    }
}

I use the debugger in this code to see that the file types I want aren't available.

Toby 1 Kenobi
  • 4,717
  • 2
  • 29
  • 43

1 Answers1

0

The SPIs should be registered using the ServiceLoader mechanism, which the jars you downloaded don't seem to do. That is probably a bug in their packaging.

I have never used jflac, but it looks like you should register:

  • org.kc7bfi.jflac.sound.spi.FlacAudioFileReader as a javax.sound.sampled.spi.AudioFileReader and
  • org.kc7bfi.jflac.sound.spi.FlacFormatConversionProvider as a javax.sound.sampled.spi.FormatConversionProvider.

The source repository actually contains the necessary service files. Add these to your resources under META-INF/services.

jackrabbit
  • 5,525
  • 1
  • 27
  • 38
  • Thanks! But how do I add those resources? I tried putting them into the jar files, then deleting my wrapper module and re-making it, but that didn't work. In the wrapper module I can see the jars under `libraries` and I can expand each one to find the `META-INF` node but no `services` nodes are under these, only a manifest file. – Toby 1 Kenobi Nov 25 '14 at 09:05
  • Also under the `source packages` node of the jflac module I created a new folder `META-INF` and in that created a folder `services` then, outside the IDE, copied the two files into that folder. But still it doesn't work. – Toby 1 Kenobi Nov 25 '14 at 09:51
  • @Toby1Kenobi [This](http://stackoverflow.com/q/16633244/3707) seems to indicate that you add the files to your source dir under `META-INF/services`. – jackrabbit Nov 25 '14 at 20:54
  • The whole project is a netbeans platform project which doesn't have a `src` folder. It is a collection of modules and each module is like its own project there is a folder for each under my main project folder and each has a `src` folder. I put the files under the `src` folder of the jflac wrapper module, under `META-INF/services` and it still doesn't work. – Toby 1 Kenobi Nov 26 '14 at 16:38
  • The only thing that is relevant is that, in your final project, the ClassLoader can see those two service files and the classes they refer to. I don't know how to do that in a NetBeans app. Does [this question](http://stackoverflow.com/q/3142515/3707) and its answers help you? – jackrabbit Nov 26 '14 at 21:25
  • No, that doesn't seem to suggest anything that I'm not already doing. My research suggests the `META-INF/services` structure should be in the root of the jar, which is the first thing I tried. I should point out that what I'm doing also doesn't work with [MP3 SPI](http://www.javazoom.net/mp3spi/sources.html) and [Vorbis SPI](http://www.javazoom.net/vorbisspi/sources.html), both of which have the service files in the correct location of their jar files. Could it be anything to do with the code name base I give the wrapper modules? – Toby 1 Kenobi Nov 27 '14 at 05:22
  • I've tried adding the mp3spi jars to to the module that is using them (as wrapped jars) instead of having a separate wrapper module, but it's still not working. – Toby 1 Kenobi Nov 27 '14 at 06:25
  • One last try [from the Netbeans docs](http://bits.netbeans.org/dev/javadoc/org-openide-util-lookup/org/openide/util/lookup/ServiceProvider.html), but I'm already far beyond my comfort zone. If this does not help, ask a new question on this site about using ServiceLoader in the context of Netbeans. – jackrabbit Nov 27 '14 at 06:49
  • thanks for trying. I've posted a [new question](http://stackoverflow.com/questions/27182184/how-do-i-use-serviceloader-in-a-netbeans-platform-project-to-add-spis) as you suggested. – Toby 1 Kenobi Nov 28 '14 at 05:01