Skip to content

Getting JMF to work with Java Web Start

by werner on December 3rd, 2008

The Java Media Framework (JMF) from Sun allows to play and process video and audio media with Java.

JMF can sometimes be a beast. It’s very hard to get it to work with Java Web Start applications because of the very peculiar way in which JMF accesses its configuration file jmf.properties.

The file jmf.properties is essential to JMF. Without it, JMF can not associate multiplexers and codecs to media objects. JMF looks for this file in directories denoted by the Java class path. Unfortunately the class path of Java Web Start applications only consists of JAR files. Since a JAR file is not a directory, JMF can not find the jmf.properties file, and thus can not function.

I dug a bit in the code of JMF, and found a way to load the jmf.properties by myself. Here is a code snippet showing how to locate and load it from inside the JAR file of my Java Web Start application:

import com.sun.media.util.Registry;
import java.io.*;
import java.util.*;

public class JMFInitializer {
    public static boolean initJMF() {
        boolean success = false;
        InputStream in = null;
        try {
            in = ViewRegistry.class.getResourceAsStream("/jmf.properties");
            success = readJMFRegistry(in);
        } finally {
           if (in != null) {
               try {
                   in.close();
               } catch (IOException e) {}
           }
        }
        return success;
    }

    private static boolean readJMFRegistry(InputStream ris) {
        if (ris == null) {
            return false;
        }

        try {
            ObjectInputStream ois = new ObjectInputStream(ris);

            int tableSize = ois.readInt();
            int version = ois.readInt();
            if (version > 200) {
                System.err.println("Version number mismatch.nThere could be" +
" errors in reading the registry");
            }
            HashMap<String, Object> hash = new HashMap<String, Object>();
            for (int i = 0; i < tableSize; i++) {
                String key = ois.readUTF();
                boolean failed = false;
                byte[] serObject;
                try {
                     Object value = ois.readObject();
                     hash.put(key, value);
                } catch (ClassNotFoundException cnfe) {
                     failed = true;
                } catch (OptionalDataException ode) {
                     failed = true;
                }
            }
            ois.close();
            ris.close();
            for (Map.Entry<String, Object> entry : hash.entrySet()) {
                 Registry.set(entry.getKey(), entry.getValue());
             }

         } catch (IOException ioe) {
             System.err.println("IOException in readJMFRegistry: " + ioe);
             return false;
         } catch (Throwable t) {
             return false;
         }
         return true;
    }
}

From → Java

One Comment

Trackbacks & Pingbacks

  1. JMF in un applet : S3ldonPress

Comments are closed.