为什么要对视频源地址进行加密播放呢。
主要是因为像百度APP,QQ浏览器等,带有扩展插件,能自动获取到网页中的视频并下载,此功能就是防止其获取视频并下载的。
先是HTML代码如下:
<video id="my-video" class="video-js" playsinline controls preload="auto" controlslist="nodownload" controlslist="nofullscreen" width="100%" poster="缩图地址" data-setup="{}"> <source src="" type="video/mp4"> <source src="" type="video/ogg"> <source src="" type="video/webm"> <p class="vjs-no-js"> To view this video please enable JavaScript, and consider upgrading to a web browser that <a href="http://videojs.com/html5- video-support/" target="_blank">supports HTML5 video</a> </p> </video>
然后是JS代码
<script type="text/javascript"> var video = document.getElementById("my-video"); window.URL = window.URL || window.webkitURL; var xhr = new XMLHttpRequest(); var play_url = '/aaaaa.mp4'; //这里的地址只能使用本地地址,且视频不能太大 xhr.open("GET", play_url, true); xhr.responseType = "blob"; xhr.onload = function() { if (this.status == 200) { var blob = this.response; console.log(blob); video.onload = function(e) { window.URL.revokeObjectURL(video.src); }; video.src = window.URL.createObjectURL(blob); } } xhr.send(); </script>