溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在Html5使用數(shù)據(jù)流播放視頻

發(fā)布時間:2022-02-23 11:12:55 來源:億速云 閱讀:611 作者:小新 欄目:開發(fā)技術

這篇文章主要為大家展示了“怎么在Html5使用數(shù)據(jù)流播放視頻”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“怎么在Html5使用數(shù)據(jù)流播放視頻”這篇文章吧。

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,為具體的視頻文件路徑。當將訪問請求變?yōu)間etVideo.do?fileId=xxx 這種形式,服務端返回字節(jié)流的時候后端實現(xiàn)需要一些更改。

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

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目前獲取視頻的時候請求頭會帶一個與斷點續(xù)傳有關的信息。對于ios來說,他不是一次性請求全部文件的,一般首先會請求0-1字節(jié),這個會寫在request header的"range"字段中:range:‘bytes=0-1’。
而服務端必須滿足range的要求:解析range字段,然后按照range字段的要求返回對應的數(shù)據(jù)。

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

  • Content-Type:明確指定視頻格式,有"video/mp4", “video/ogg”, "video/mov"等等。

  • Content-Range:格式是 “bytes <start>-<end>/<total>”,其中start和end必需對應request header里的range字段,total是文件總大小。

  • Content-Length:返回的二進制長度。

斷點續(xù)傳實現(xiàn)如下:

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:從某字節(jié)開始的下載(bytes=27000-);2:從某字節(jié)開始到某字節(jié)結束的下載(bytes=27000-39000)  
 long contentLength = 0;// 客戶端請求的字節(jié)總量  
 String rangeBytes = "";// 記錄客戶端傳來的形如“bytes=27000-”或者“bytes=27000-39000”的內容  
 RandomAccessFile raf = null;// 負責讀取數(shù)據(jù)  
 OutputStream os = null;// 寫出數(shù)據(jù)  
 OutputStream out = null;// 緩沖  
 int bsize = 1024;// 緩沖區(qū)大小  
 byte b[] = new byte[bsize];// 暫存容器  

 String range = request.getHeader("Range");
 int responseStatus = 206;
 if (range != null && range.trim().length() > 0 && !"null".equals(range)) {// 客戶端請求的下載的文件塊的開始字節(jié)  
  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();
 // 告訴客戶端允許斷點續(xù)傳多線程連接下載,響應的格式是:Accept-Ranges: bytes  
 response.setHeader("Accept-Ranges", "bytes");
 // 如果是第一次下,還沒有斷點續(xù)傳,狀態(tài)是默認的 200,無需顯式設置;響應的格式是:HTTP/1.1  

 if (rangeSwitch != 0) {
  response.setStatus(responseStatus);
  // 不是從最開始下載,斷點下載響應號為206  
  // 響應的格式是:  
  // Content-Range: bytes [文件塊的開始字節(jié)]-[文件的總大小 - 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;// 實際輸出字節(jié)數(shù)  
   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;// 記錄已讀字節(jié)數(shù)  
     while (readLength <= contentLength - bsize) {// 大部分字節(jié)在這里讀取  
      n = raf.read(b);
      readLength += n;
      out.write(b, 0, n);
      outLength += n;
     }
     if (readLength <= contentLength) {// 余下的不足 1024 個字節(jié)在這里讀取  
      n = raf.read(b, 0, (int) (contentLength - readLength));
      out.write(b, 0, n);
      outLength += n;
     }
     break;
    }
    default: {
     break;
    }
   }
   System.out.println("Content-Length為:" + contentLength + ";實際輸出字節(jié)數(shù):" + 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="h6" 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>

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

以上是“怎么在Html5使用數(shù)據(jù)流播放視頻”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI