婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > Html5通過數據流方式播放視頻的實現

Html5通過數據流方式播放視頻的實現

熱門標簽:高德地圖標注廁所 西安金倫外呼系統 江西ai電銷機器人如何 中國地圖標注城市的 威海語音外呼系統平臺 地圖標注員工作內容 通遼地圖標注app 智能語音電銷機器人客戶端 地圖標注沿海城市房價

本文介紹如何通過H5頁面通過數據流的方式播放服務端的視頻文件,可以兼容PC、Android和IOS環境。

H5頁面可以通過<video> 標簽來播放視頻。一般的方式如下:

<!DOCTYPE HTML>
<html>
<body>

<video src="/i/movie.mp4" controls="controls">
your browser does not support the video tag
</video>

</body>
</html>

src中指定了要播放的視頻的URL,為具體的視頻文件路徑。當將訪問請求變為getVideo.do?fileId=xxx 這種形式,服務端返回字節流的時候后端實現需要一些更改。

一般的方式是讀本地文件然后寫到response中,代碼實現如下:

public void downFile(File downloadFile, 
      HttpServletResponse response, 
      HttpServletRequest request) throws Exception {
 response.reset();
 response.setContentType("video/mp4;charset=UTF-8"); 
 
 InputStream in = null;
 ServletOutputStream out = null;
 try { 
  out = response.getOutputStream();
  
  in = new FileInputStream(downloadFile);
  if(in !=null){
    byte[] b = new byte[1024];  
     int i = 0;  
     while((i = in.read(b)) > 0){  
    out.write(b, 0, i);  
     }  
     out.flush();   
     in.close(); 
   
  }
 } catch (Exception e) {
  
   e.printStackTrace();
 
 }finally{
  if(in != null) {  
   try { in.close(); } catch (IOException e) { }  
   in = null;  
  } 
  if(out != null) {  
   try { out.close(); } catch (IOException e) { }  
   out = null;  
  } 
 }
}

這種方式在PC端和Android手機上都能正常顯示,但在IOS手機上通過Safari瀏覽器就不能播放。ios目前獲取視頻的時候請求頭會帶一個與斷點續傳有關的信息。對于ios來說,他不是一次性請求全部文件的,一般首先會請求0-1字節,這個會寫在request header的"range"字段中:range:‘bytes=0-1’。
而服務端必須滿足range的要求:解析range字段,然后按照range字段的要求返回對應的數據。

在響應頭中response header至少要包含三個字段:

  • Content-Type:明確指定視頻格式,有"video/mp4", “video/ogg”, "video/mov"等等。
  • Content-Range:格式是 “bytes <start>-<end>/<total>”,其中start和end必需對應request header里的range字段,total是文件總大小。
  • Content-Length:返回的二進制長度。

斷點續傳實現如下:

public void downRangeFile(File downloadFile, 
       HttpServletResponse response, 
       HttpServletRequest request) throws Exception {

 if (!downloadFile.exists()) {
  response.sendError(HttpServletResponse.SC_NOT_FOUND);
  return;
 }

 long fileLength = downloadFile.length();// 記錄文件大小  
 long pastLength = 0;// 記錄已下載文件大小  
 int rangeSwitch = 0;// 0:從頭開始的全文下載;1:從某字節開始的下載(bytes=27000-);2:從某字節開始到某字節結束的下載(bytes=27000-39000)  
 long contentLength = 0;// 客戶端請求的字節總量  
 String rangeBytes = "";// 記錄客戶端傳來的形如“bytes=27000-”或者“bytes=27000-39000”的內容  
 RandomAccessFile raf = null;// 負責讀取數據  
 OutputStream os = null;// 寫出數據  
 OutputStream out = null;// 緩沖  
 int bsize = 1024;// 緩沖區大小  
 byte b[] = new byte[bsize];// 暫存容器  

 String range = request.getHeader("Range");
 int responseStatus = 206;
 if (range != null && range.trim().length() > 0 && !"null".equals(range)) {// 客戶端請求的下載的文件塊的開始字節  
  responseStatus = javax.servlet.http.HttpServletResponse.SC_PARTIAL_CONTENT;
  System.out.println("request.getHeader(\&;Range\&;)=" + range);
  rangeBytes = range.replaceAll("bytes=", "");
  if (rangeBytes.endsWith("-")) {
   rangeSwitch = 1;
   rangeBytes = rangeBytes.substring(0, rangeBytes.indexOf('-'));
   pastLength = Long.parseLong(rangeBytes.trim());
   contentLength = fileLength - pastLength;
  } else {
   rangeSwitch = 2;
   String temp0 = rangeBytes.substring(0, rangeBytes.indexOf('-'));
   String temp2 = rangeBytes.substring(rangeBytes.indexOf('-') + 1, rangeBytes.length());
   pastLength = Long.parseLong(temp0.trim());
  }
 } else {
  contentLength = fileLength;// 客戶端要求全文下載  
 }

 
 // 清除首部的空白行  
 response.reset();
 // 告訴客戶端允許斷點續傳多線程連接下載,響應的格式是:Accept-Ranges: bytes  
 response.setHeader("Accept-Ranges", "bytes");
 // 如果是第一次下,還沒有斷點續傳,狀態是默認的 200,無需顯式設置;響應的格式是:HTTP/1.1  

 if (rangeSwitch != 0) {
  response.setStatus(responseStatus);
  // 不是從最開始下載,斷點下載響應號為206  
  // 響應的格式是:  
  // Content-Range: bytes [文件塊的開始字節]-[文件的總大小 - 1]/[文件的總大小]  
  switch (rangeSwitch) {
   case 1: {
    String contentRange = new StringBuffer("bytes ")
      .append(new Long(pastLength).toString()).append("-")
      .append(new Long(fileLength - 1).toString())
      .append("/").append(new Long(fileLength).toString())
      .toString();
    response.setHeader("Content-Range", contentRange);
    break;
   }
   case 2: {
    String contentRange = range.replace("=", " ") + "/"
      + new Long(fileLength).toString();
    response.setHeader("Content-Range", contentRange);
    break;
   }
   default: {
    break;
   }
  }
 } else {
  String contentRange = new StringBuffer("bytes ").append("0-")
    .append(fileLength - 1).append("/").append(fileLength)
    .toString();
  response.setHeader("Content-Range", contentRange);
 }

 try {
  response.setContentType("video/mp4;charset=UTF-8"); 
  response.setHeader("Content-Length", String.valueOf(contentLength));
  os = response.getOutputStream();
  out = new BufferedOutputStream(os);
  raf = new RandomAccessFile(downloadFile, "r");
  try {
   long outLength = 0;// 實際輸出字節數  
   switch (rangeSwitch) {
    case 0: {
    }
    case 1: {
     raf.seek(pastLength);
     int n = 0;
     while ((n = raf.read(b)) != -1) {
      out.write(b, 0, n);
      outLength += n;
     }
     break;
    }
    case 2: {
     raf.seek(pastLength);
     int n = 0;
     long readLength = 0;// 記錄已讀字節數  
     while (readLength <= contentLength - bsize) {// 大部分字節在這里讀取  
      n = raf.read(b);
      readLength += n;
      out.write(b, 0, n);
      outLength += n;
     }
     if (readLength <= contentLength) {// 余下的不足 1024 個字節在這里讀取  
      n = raf.read(b, 0, (int) (contentLength - readLength));
      out.write(b, 0, n);
      outLength += n;
     }
     break;
    }
    default: {
     break;
    }
   }
   System.out.println("Content-Length為:" + contentLength + ";實際輸出字節數:" + outLength);
   out.flush();
  } catch (IOException ie) {
   // ignore  
  }
 } catch (Exception e) {
  e.printStackTrace();
 } finally {
  if (out != null) {
   try {
    out.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  if (raf != null) {
   try {
    raf.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
}

H5頁面:

<!DOCTYPE HTML>
<html>
<body>


<video width="100%" height="200" rel="preload" x5-video-player-type="h5" playsinline="true" webkit-playsinline="true" controls="controls">
<source src="http://127.0.0.1:8080/XXX/getVideo.do?fileId=16" type="video/mp4">
</video>

</script>
</body>
</html>

通過上述斷點續傳方式H5可正常播放視頻數據流,并且支持各種平臺。

到此這篇關于Html5通過數據流方式播放視頻的實現的文章就介紹到這了,更多相關Html5數據流播放視頻內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章,希望大家以后多多支持腳本之家!

標簽:晉中 河池 北海 眉山 阜陽 崇左 營口 青海

巨人網絡通訊聲明:本文標題《Html5通過數據流方式播放視頻的實現》,本文關鍵詞  Html5,通過,數據流,方式,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Html5通過數據流方式播放視頻的實現》相關的同類信息!
  • 本頁收集關于Html5通過數據流方式播放視頻的實現的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 泸定县| 大埔县| 灵宝市| 长武县| 尖扎县| 岱山县| 嵊州市| 志丹县| 周宁县| 临潭县| 英山县| 阿克苏市| 怀集县| 武功县| 江孜县| 西林县| 甘谷县| 延川县| 万安县| 南宁市| 嘉荫县| 沂水县| 沙田区| 揭阳市| 垣曲县| 东乡族自治县| 台东市| 嘉义市| 尚志市| 嵩明县| 额敏县| 如东县| 武宁县| 错那县| 洛南县| 阿瓦提县| 英吉沙县| 晋中市| 姚安县| 台南县| 榆中县|