10

I am using JavaFX media player to play an mp3 file using following code

 new MediaPlayer(new Media(FileObject.toURI().toString())).play();

However now I have a requirement that I have the mp3 byte data in memory instead of an File Object. The reason is the mp3 file is encrypted and then shipped along with the program. Hence I need to decrypt the mp3 file in memory or input stream.

I could decrypt the mp3 file to an temporary file in temp directory but this would be a performance overhead and the audio content would be insecure.

2 Answers 2

2

From the Media Javadoc

Only HTTP, FILE, and JAR URLs are supported. If the provided URL is invalid then an exception will be thrown. If an asynchronous error occurs, the error property will be set. Listen to this property to be notified of any such errors.

I'm not personally familiar with JavaFX, but that would suggest to me that without resorting to nasty hacks, you're not going to be able to read media directly from memory. Normally for this kind of URI only interface I'd suggest registering a custom UrlStreamHandler and a custom protocol that reads from memory. But assuming that JavaDoc is correct, the JavaFX uses it's own resolution so presumably this will not work.

Given this then I suspect the only way to make this work is to provide access to the in-memory MP3 over HTTP. You could do this using Jetty or any similar embeddable servlet container. Something along the following lines:

1) Start Up Jetty as per Quick Start Guide

2) Register a servlet that looks something like below. This servlet will expose your in-memory data:

public class MagicAccessServlet extends HttpServlet {
    private static final Map<String, byte[]> mediaMap = new ConcurrentHashMap();

    public static String registerMedia(byte[] media) {
        String key = UUID.randomUUID().toString();
        mediaMap.put(key, media);
        return key;
    }

    public static deregisterMedia(String key) {
        mediaMap.remove(key);
    }

    public void doGet(HttpServletRequest req, HttpServletResponse resp) {
        String key = req.get("key");
        byte[] media = mediaMap.get(key);
        resp.setContentLength(media.length);
        resp.getOutputStream().write(media);
    }
}

Then you can access from within your application using an http url. E.g. something like

MagicAccessServlet.registerMedia(decodedMp3);
new MediaPlayer(new Media("http://localhost:<port>/<context>/<servlet>?key=" + key)).play();    
1
0

Unfortunately as the Media constructor stands I see no easy way to do this other than the temporary file approach. Note that while I agree the performance would have an overhead, if the files aren't too big (most mp3 files generally aren't) then the overhead should be minimal in this sense. And technically, decoding the content to memory also renders it insecure (though admittedly much harder to extract.)

One slightly crazy approach I did think of was to use sockets. You could setup a separate part of your application which decrypts the unencrypted content and then streams the raw mp3 bytes over a certain port on localhost. You could then provide this as a HTTP URI to the Media constructor.

1
  • With Decoding the Content to Memory you mean the temp file on the hard disk?
    – El Mac
    Dec 3, 2015 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.