Displaying Binary Multimedia Content with PrimeFaces

PrimeFaces p:media component is a generic multimedia player supporting various formats like flash, windows media, quicktime and real player. Suppose you have a mov file located at your application’s webroot. This file can be displayed as;

Basic Usage

<p:media value="/multimedia/dramatic_praire_dog.mov" />

By default player is calculated based on the file extension, still you can also choose a specific player;

<p:media value="http://www.youtube.com/watch?v=jHjFxJVeCQs" player="flash"/>

Binary Content
This is where things get interesting, suppose you keep your multimedia files in a database with binary format. Using PrimeFaces StreamedContent API, it’s easy to display them as well.

public class Bean {
    
    private StreamedContent media;

    public Bean() {
        InputStream stream = //get inputstream from datatabase record
        media = new DefaultStreamedContent(stream, "video/quicktime");
    }

    public StreamedContent getMedia() { return media; }
}
<p:media value="#{bean.media}" />

That’s it, by providing a streamedcontent instance, p:media can display the binary file easily. This is actually the same process for displaying binary images, see my Dynamic Images with JSF tutorial for more information.

Live Demo
Live demo of p:media is at PrimeFaces Online Showcase.

One Response to Displaying Binary Multimedia Content with PrimeFaces

  1. Oleg says:

    Hello Cagatay,

    Some contents cannot be displayed embedded. For instance, Firefox/Safari/Chrome don’t have a plugin for MS Office documents. What happens in this case? Will be document displayed extern in an external browser window?

%d bloggers like this: