美文网首页视频处理
IOS获取视频流的处理(断点续传)

IOS获取视频流的处理(断点续传)

作者: Hush____ | 来源:发表于2018-07-13 17:51 被阅读219次

    最近在支持moa的开发,给它们写了大概十多个接口,其中有一个接口是获取视频流。当时没做过,就直接给的是,pc端播放正常的流给moa端使用,结果对方测试反馈过来,不能播放。很蛋疼,咨询了公司大牛,寻求了帮助。首先,断点续传,要设置下头Rang,并且要规定流的长度,再设置下contenttype,根据文件后缀去做适配。最后在web.xml中配置下mine-mapping标签。

    java代码:

    /**
     * 文件下载
     * @param request
     * @param response
     * @param filePath
     * @throws Exception
     */
    @RequestMapping(value = "/getImageVideo/Users/pengcheng/test/{fileName:.+}", method = RequestMethod.GET)
    public static void getImageVideo(HttpServletRequest request,
            HttpServletResponse response,@PathVariable String fileName) throws Exception{
        try {
            BufferedInputStream bis = null;
            String savePath = ConstantUtil.FILEPATH;
            File file = new File(savePath + fileName);
                if (file.exists()) {
                    long p = 0L;
                    long toLength = 0L;
                    long contentLength = 0L;
                    int rangeSwitch = 0; // 0,从头开始的全文下载;1,从某字节开始的下载(bytes=27000-);2,从某字节开始到某字节结束的下载(bytes=27000-39000)
                    long fileLength;
                    String rangBytes = "";
                    fileLength = file.length();
         
                    // get file content
                    InputStream ins = new FileInputStream(file);
                    bis = new BufferedInputStream(ins);
         
                    // tell the client to allow accept-ranges
                    response.reset();
                    response.setHeader("Accept-Ranges", "bytes");
         
                    // client requests a file block download start byte
                    String range = request.getHeader("Range");
                    if (range != null && range.trim().length() > 0 && !"null".equals(range)) {
                        response.setStatus(javax.servlet.http.HttpServletResponse.SC_PARTIAL_CONTENT);
                        rangBytes = range.replaceAll("bytes=", "");
                        if (rangBytes.endsWith("-")) {  // bytes=270000-
                            rangeSwitch = 1;
                            p = Long.parseLong(rangBytes.substring(0, rangBytes.indexOf("-")));
                            contentLength = fileLength - p;  // 客户端请求的是270000之后的字节(包括bytes下标索引为270000的字节)
                        } else { // bytes=270000-320000
                            rangeSwitch = 2;
                            String temp1 = rangBytes.substring(0, rangBytes.indexOf("-"));
                            String temp2 = rangBytes.substring(rangBytes.indexOf("-") + 1, rangBytes.length());
                            p = Long.parseLong(temp1);
                            toLength = Long.parseLong(temp2);
                            contentLength = toLength - p + 1; // 客户端请求的是 270000-320000 之间的字节
                        }
                    } else {
                        contentLength = fileLength;
                    }
         
                    // 如果设设置了Content-Length,则客户端会自动进行多线程下载。如果不希望支持多线程,则不要设置这个参数。
                    // Content-Length: [文件的总大小] - [客户端请求的下载的文件块的开始字节]
                    response.setHeader("Content-Length", new Long(contentLength).toString());
         
                    // 断点开始
                    // 响应的格式是:
                    // Content-Range: bytes [文件块的开始字节]-[文件的总大小 - 1]/[文件的总大小]
                    if (rangeSwitch == 1) {
                        String contentRange = new StringBuffer("bytes ").append(new Long(p).toString()).append("-")
                                .append(new Long(fileLength - 1).toString()).append("/")
                                .append(new Long(fileLength).toString()).toString();
                        response.setHeader("Content-Range", contentRange);
                        bis.skip(p);
                    } else if (rangeSwitch == 2) {
                        String contentRange = range.replace("=", " ") + "/" + new Long(fileLength).toString();
                        response.setHeader("Content-Range", contentRange);
                        bis.skip(p);
                    } else {
                        String contentRange = new StringBuffer("bytes ").append("0-")
                                .append(fileLength - 1).append("/")
                                .append(fileLength).toString();
                        response.setHeader("Content-Range", contentRange);
                    }
                    // MS IE5.5 有要作特别处理
                    //response.setContentType("video/mp4");
                    String contentType = "application/octet-stream";
                    if(fileName.indexOf(".")>1&&StringUtils.isNotBlank(fileName.substring(fileName.lastIndexOf(".")+1,fileName.length())))
                    {
                            String later = "";
                            if(StringUtils.isNoneBlank(fileName)) {
                                later = fileName.substring(fileName.lastIndexOf(".") + 1,fileName.length());
                            };
                            String mimeType = PropertyUtil.getValue(later);
                        
                        contentType = StringUtils.isNotBlank(mimeType)?mimeType:contentType;
                    }
                    response.setContentType(contentType);
                 
                    if (request.getHeader("User-Agent").indexOf("MSIE 5.5") != -1) {
                        response.setHeader("Content-Disposition", "filename=" + fileName);
                    } else { // 非 IE5.5 的 Header 设定方式,IE 5.5 不能加attachment
                        response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
                    }
                   
         
                    OutputStream out = response.getOutputStream();
                    int n = 0;
                    long readLength = 0;
                    int bsize = 1024;
                    byte[] bytes = new byte[bsize];
                    if (rangeSwitch == 2) {
                        // 针对 bytes=27000-39000 的请求,从27000开始写数据                    
                        while (readLength <= contentLength - bsize) {
                            n = bis.read(bytes);
                            readLength += n;
                            out.write(bytes, 0, n);
                        }
                        if (readLength <= contentLength) {
                            n = bis.read(bytes, 0, (int) (contentLength - readLength));
                            out.write(bytes, 0, n);
                        }                   
                    } else {
                        while ((n = bis.read(bytes)) != -1) {
                            out.write(bytes,0,n);                                                      
                        }                   
                    }
                    out.flush();
                    out.close();
                    bis.close();
                } 
            } catch (IOException ie) {
                // 忽略 ClientAbortException 之类的异常
            } catch (Exception e) {
                logger.error(e);
            }
    }
    

    xml中,视频文件后缀适配:

    mov=video/quicktime
    movie=video/x-sgi-movie
    mp1=audio/x-mpeg
    mp2=audio/x-mpeg
    mp3=audio/x-mpeg
    mp4=video/mp4
    mpa=audio/x-mpeg
    mpe=video/mpeg
    MOV=video/quicktime
    MOVIE=video/x-sgi-movie
    MP1=audio/x-mpeg
    MP2=audio/x-mpeg
    MP3=audio/x-mpeg
    MP4=video/mp4
    MPA=audio/x-mpeg
    MPE=video/mpeg
    

    web.xml中,配置下mine-mapping标签:

     <mime-mapping>
        <extension>mov</extension>
        <mime-type>video/quicktime</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>movie</extension>
        <mime-type>video/x-sgi-movie</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>mp1</extension>
        <mime-type>audio/x-mpeg</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>mp2</extension>
        <mime-type>audio/x-mpeg</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>mp3</extension>
        <mime-type>audio/x-mpeg</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>mp4</extension>
        <mime-type>video/mp4</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>mpa</extension>
        <mime-type>audio/x-mpeg</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>mpe</extension>
        <mime-type>video/mpeg</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>MOV</extension>
        <mime-type>video/quicktime</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>MOVIE</extension>
        <mime-type>video/x-sgi-movie</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>MP1</extension>
        <mime-type>audio/x-mpeg</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>MP2</extension>
        <mime-type>audio/x-mpeg</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>MP3</extension>
        <mime-type>audio/x-mpeg</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>MP4</extension>
        <mime-type>video/mp4</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>MPA</extension>
        <mime-type>audio/x-mpeg</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>MPE</extension>
        <mime-type>video/mpeg</mime-type>
    </mime-mapping>   
    

    相关文章

      网友评论

        本文标题:IOS获取视频流的处理(断点续传)

        本文链接:https://www.haomeiwen.com/subject/vnfspftx.html