html5 video標簽播放視頻流


從文件服務器讀取音視頻文件,以流的方式傳給前台,並能夠播放視頻。

做了一個demo,用html5的video,audio標簽實現。

后台實現代碼:

@GetMapping(value = "/getVideos")
public String getVideos(HttpServletRequest request, HttpServletResponse response)
{
    try {
        FileInputStream fis = null;
        OutputStream os = null ;
        fis = new FileInputStream("C:\\Users\\zhangxin\\Desktop\\douyin.mp4");
        int size = fis.available(); // 得到文件大小
        byte data[] = new byte[size];
        fis.read(data); // 讀數據
        fis.close();
        fis = null;
        response.setContentType("video/mp4"); // 設置返回的文件類型
        os = response.getOutputStream();
        os.write(data);
        os.flush();
        os.close();
        os = null;
 
 
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

前端實現代碼:

<video width="1120" height="540" controls="controls" id="video" preload="auto"     >
    <source src="getVideos"   type="video/mp4">
</video>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM