【问题标题】:Not able to play video using html Mediasource无法使用 html Mediasource 播放视频
【发布时间】:2017-05-25 01:42:17
【问题描述】:

我有一个带有 kcinit.mp4、kc1.m4s、kc2.m4s、kc3.m4s ... 直到 kc54.m4s 的服务器。我有一个 java servlet 来响应对这个文件的 GET 请求。我正在尝试实现流式传输。但是,我无法使用 Mediasource 在 html 上播放这些文件。

http://localhost:8080/Virtual_Cinema2/Page - 访问 HTML 页面

http://localhost:8080/Virtual_Cinema2/kc?type=init - 获取 kcinit.mp4

http://localhost:8080/Virtual_Cinema2/kc?type=vid&count=1 - 获取 kc1.m4s

小服务程序:

package vc.servlets;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/kc")
public class Thozha extends HttpServlet {
    private static final long serialVersionUID = 1L;

        public Thozha() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String type = request.getParameter("type") ;
        StringBuffer sb = new StringBuffer() ;
        OutputStream os = response.getOutputStream() ;
        byte [] buffer = new byte[4096] ;
        int length ;
        if(type.equals("vid")) {
            System.out.println("vid");
            String count = request.getParameter("count") ;
            sb.append("D:/videos/kc") ;
            sb.append(count) ;
            sb.append(".m4s") ;
            File file = new File(sb.toString()) ;
            if(file.exists()) {
                FileInputStream fis = new FileInputStream(file) ;
                response.setStatus(HttpServletResponse.SC_OK);
                response.setContentType("video/mp4") ;
                response.setContentLengthLong(file.length());
                while((length = fis.read(buffer)) > 0) {
                    os.write(buffer, 0, length) ;
                }
                os.flush() ;
                fis.close() ;
            }
        }
        else if(type.equals("init")) {
            System.out.println("init");
            File file = new File("D:/videos/kcinit.mp4") ;
            if(file.exists()) {
                FileInputStream fis = new FileInputStream(file) ;
                response.setStatus(HttpServletResponse.SC_OK);
                response.setContentType("video/mp4") ;
                response.setContentLengthLong(file.length());
                while((length = fis.read(buffer)) > 0) {
                    os.write(buffer, 0, length) ;
                }
                os.flush() ;
                fis.close() ;
            }
        }
        else {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }
    }

}

HTML 页面:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p id="p">Hello</p>
<video id="video" height="300" width="300" controls>  
  Video element is not supported in your browser
</video>
<script> 
    var count = 1 ;
    var p = document.getElementById('p');
    var video = document.getElementById('video'); 
    var mediaSource ;
    var sourceBuffer ;
    if (window.MediaSource) {
    mediaSource = new MediaSource();
    video.src = window.URL.createObjectURL(mediaSource);
    p.innerHTML = "supported" ;
    mediaSource.addEventListener('sourceopen',init(e), false) ;
    } 
    else {
    p.innerHTML = "not supported" ;
    }
    function init(e) {
       var xhr = new XMLHttpRequest(); // Set up xhr request
       xhr.open("GET", "/kc?type=init", true); // Open the request  
       xhr.send();
       xhr.responseType = 'arraybuffer';
       xhr.onreadystatechange = function() {
           if (this.readyState == 4 && this.status == 200) {
              sourceBuffer.appendBuffer(new Uint8Array(xhr.response));
              p.innerHTML = "request successful" ; 
              sourceBuffer.addEventListener("updateend",update(), false);
           }
      };
    }
    function update() {
        var xhr = new XMLHttpRequest();
        xhr.open("GET", "/kc?type=vid&count="+count, true); // Open the request  
        xhr.send();
        xhr.responseType = 'arraybuffer';
        xhr.onreadystatechange = function() {
           if (this.readyState == 4 && this.status == 200) {
             sourceBuffer.appendBuffer(new Uint8Array(xhr.response));
               if(count == 1) { //if first segment play video
                 video.play() ; 
               }
             count++ ;
           }
        };
   }
</script>
</body>
</html>

我正在使用 Microsoft Edge 测试页面。我明白了(p 表示支持,并且视频元素永远加载):

【问题讨论】:

    标签: java html servlets media-source


    【解决方案1】:

    很抱歉提出这个问题。我犯了一些粗心的错误。 servlet 没有任何问题。错误是由于 HTML 页面造成的。

    html页面的新代码:

    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
    <p id="p">Hello</p>
    <video id="video" height="300" width="300" controls>  
      Video element is not supported in your browser
    </video>
    <script> 
        var count = 1 ;
        var p = document.getElementById('p');
        var video = document.getElementById('video'); 
        var mediaSource ;
        var sourceBuffer ;
        if (window.MediaSource && MediaSource.isTypeSupported("video/mp4")) {
        mediaSource = new MediaSource();
        video.src = window.URL.createObjectURL(mediaSource);
        p.innerHTML = "1" ;
        mediaSource.addEventListener('sourceopen',init, false) ;
        } 
        else {
        p.innerHTML = "not supported" ;
        }
        function init(e) {
           p.innerHTML = "2" ;
           sourceBuffer = mediaSource.addSourceBuffer("video/mp4");
           var xhr = new XMLHttpRequest(); // Set up xhr request
           xhr.open("GET", "kc?type=init", true); // Open the request  
           xhr.send();
           xhr.responseType = 'arraybuffer';
           xhr.onreadystatechange = function() {
               if (this.readyState == 4 && this.status == 200) {
                  sourceBuffer.appendBuffer(new Uint8Array(xhr.response));
                  p.innerHTML = "request successful" ; 
                  sourceBuffer.addEventListener("updateend",update, false);
               }
          };
        }
        function update() {
            p.innerHTML = "3" ;
            var xhr = new XMLHttpRequest();
            xhr.open("GET", "kc?type=vid&count="+count, true); // Open the request  
            xhr.send();
            xhr.responseType = 'arraybuffer';
            xhr.onreadystatechange = function() {
               if (this.readyState == 4 && this.status == 200) {
                 sourceBuffer.appendBuffer(new Uint8Array(xhr.response));
                   if(count == 1) { //if first segment play video
                     video.play() ; 
                   }
                 count++ ;
               }
            };
       }
    </script>
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-19
      • 2016-10-06
      • 2020-03-04
      相关资源
      最近更新 更多