使用p:media在PrimeFaces中动态流式传输内容

时间:2015-04-14 19:51:40

标签: jsf jsf-2 primefaces media

我正在尝试实现一个功能,我必须使用动态生成一个流内容 PrimeFaces p:媒体标签。

要求是此内容可以是需要在p:media标签中显示/流式传输的pdf或视频文件。

现在,我可以成功动态流式传输pdf内容类型,并在浏览器中显示pdf文档。

但是对于相同的代码,我无法打开视频流。如果所选类型是视频(例如avi,其中播放器是windows, 媒体播放器开放,说“开放媒体”,然后没有任何反应。 flv视频相同(未在浏览器中加载电影)

我还提供了日志消息,显示该流不为null,并且正确设置了mime类型。

我已经阅读了p:media标签的文档,并提出了一段代码:

以下是我编写的示例代码:

XHTML:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
            xmlns:ui="http://java.sun.com/jsf/facelets"
            xmlns:h="http://java.sun.com/jsf/html"
            xmlns:f="http://java.sun.com/jsf/core"
            xmlns:p="http://primefaces.org/ui"
            xmlns:fn="http://java.sun.com/jsp/jstl/functions"
             template="/resources/templates/contentTemplate.xhtml>
<ui:define name="linked-content">
    <p:panelGrid style="width: 100%; height: 100%" id="masterPanel">
        <p:row>
            <p:column>
                <p:media value="#{mediaViewManagedBean.media}" style="width: 100%; height: 100%"  player="#{mediaViewManagedBean.player}" rendered="#{not empty mediaViewManagedBean.media}">
                </p:media>
            </p:column>
        </p:row>
    </p:panelGrid>
</ui:define>

Managed Bean:

package [redacted];


import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.ResourceBundle;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.apache.commons.io.FilenameUtils;
import org.[redacted].core.domain.model.MediaMaster;
import org.[redacted].web.context.constants.ApplicationConstant;
import org.[redacted].web.context.util.AbstractManagedBeanSupport;
import org.[redacted].web.context.util.FacesUtils;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.springframework.util.StringUtils;

/**
 *
 * @author Saikat Mukherjee <saikat44@gmail.com>
 */
@ManagedBean
@RequestScoped
public class MediaViewManagedBean extends AbstractManagedBeanSupport<MediaViewManagedBean> {

private StreamedContent media;
private String player;
private static transient ResourceBundle mimeTypeResourceBundle;

private static final String[] playerTypeWindows = {"asx", "asf", "avi", "wma", "wmv"};
private static final String[] playerTypeQT = {"aif", "aiff", "aac", "au", "bmp", "gsm", "mov", "mid", "midi", "mpg", "mpeg", "mp4", "m4a", "psd", "qt", "qtif", "qif", "qti", "snd", "tif", "tiff", "wav", "3g2", "3pg"};
private static final String[] playerTypeFlash = {"flv", "mp3", "swf"};
private static final String[] playerTypeReal = {"ra", "ram", "rm", "rpm", "rv", "smi", "smil"};
private static final String[] playerTypePDF = {"pdf"};

public MediaViewManagedBean() {
    super(MediaViewManagedBean.class);
}

@PostConstruct
@Override
public void initMetaData() {

    try {
        loggingService.debug("initMetaData called");
        mimeTypeResourceBundle = ResourceBundle.getBundle(ApplicationConstant.PROP_BUNDLE_MIME_TYPE);
        /**
         * Generate a streamed media content from the media in question
         */
        MediaMaster targetMedia = (MediaMaster) FacesUtils.getHttpSession(false).getAttribute(ApplicationConstant.REQ_PARAM_TARGET_MEDIA_MASTER);
        if (targetMedia == null) {
            loggingService.info("Something is not right, the target media is null, will not generate a streamed content");
        } else {
            String mediaLink = targetMedia.getMediaLink();
            loggingService.info("target media has got a target of [" + mediaLink + "]");
            if (StringUtils.isEmpty(mediaLink)) {
                loggingService.info("mediaLink is empty, no streamed content to be generated");
            } else {
                File targetFile = new File(mediaLink);
                if (targetFile.exists() && !targetFile.isDirectory()) {
                    loggingService.info("we will now generate a streamed content for the file [" + mediaLink + "]");
                    InputStream targetFileInputStream = new BufferedInputStream(new FileInputStream(targetFile));
                    //InputStream targetFileInputStream = this.getClass().getResourceAsStream(mediaLink);
                    loggingService.info("got a not null bytestream");
                    String fileExtension = FilenameUtils.getExtension(targetFile.getName());
                    String mimeType = mimeTypeResourceBundle.getString(fileExtension);
                    loggingService.info("file extenstion [" + fileExtension + "] , mimeType [" + mimeType + "]");
                    media = new DefaultStreamedContent(targetFileInputStream, mimeType);
                    generatePlayerType(fileExtension);
                } else {
                    loggingService.info("target file [" + mediaLink + "] does not exist, will not generate a streamed content");
                }
            }
        }

    } catch (Exception e) {
        loggingService.error("Internal error:", e);
        processException(e);
    }
}

public void resetForm() {
    initMetaData();
}

private void generatePlayerType(String fileExtension) {
    if (Arrays.asList(playerTypeWindows).contains(fileExtension)) {
        player = "windows";
    }
    if (Arrays.asList(playerTypeQT).contains(fileExtension)) {
        player = "quicktime";
    }
    if (Arrays.asList(playerTypeFlash).contains(fileExtension)) {
        player = "flash";
    }
    if (Arrays.asList(playerTypeReal).contains(fileExtension)) {
        player = "real";
    }
    if (Arrays.asList(playerTypePDF).contains(fileExtension)) {
        player = "pdf";
    }
    loggingService.info("selected player type for fileExtension[" + fileExtension + "] is [" + player + "]");
}

public StreamedContent getMedia() {
    //initMetaData();
    return media;
}

public void setMedia(StreamedContent media) {
    this.media = media;
}

public String getPlayer() {
    return player;
}

public void setPlayer(String player) {
    this.player = player;
}

}

生成日志:

61681 [http-nio-8084-exec-12] DEBUG org.[redacted].web.view.admin.managedbeans.MediaViewManagedBean  - initMetaData called
61681 [http-nio-8084-exec-12] INFO  org.[redacted].web.view.admin.managedbeans.MediaViewManagedBean  - target media has got a target of [C:\Users\[redacted]\Desktop\Temp\A_[2009].avi]
61751 [http-nio-8084-exec-12] INFO  org.[redacted].web.view.admin.managedbeans.MediaViewManagedBean  - we will now generate a streamed content for the file [C:\Users\[redacted]\Desktop\Temp\A_[2009].avi]
61752 [http-nio-8084-exec-12] INFO  org.[redacted].web.view.admin.managedbeans.MediaViewManagedBean  - got a not null bytestream
61752 [http-nio-8084-exec-12] INFO  org.[redacted].web.view.admin.managedbeans.MediaViewManagedBean  - file extenstion [avi] , mimeType [video/x-msvideo,application/x-troff-msvideo,video/avi,video/msvideo]
61753 [http-nio-8084-exec-12] INFO  org.[redacted].web.view.admin.managedbeans.MediaViewManagedBean  - selected player type for fileExtension[avi] is [windows]

有人可以建议如何使视频流可运行。

PS:视频文件未损坏。

Primefaces 5.0

0 个答案:

没有答案