借用网上大神的一些知识,html5 video 视频播放都兼容(Android,iOS,电脑)

时间:2021-02-05 07:49:11
<!DOCTYPE html>
<html
xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta
http-equiv="Content-Type" content="text/html; charset=utf-8" />
   
<title></title>
</head>

<body>

<!--大概意思是在页面定义一个div用来保存js中输出的video控件,给playVideo方法传入参数来控制播放器,如果碰到在本机可以播放,但是在服务器上无法播放的问题时,请设置iis的mime类型,查看类型中是否有.mp4,没有就添加
扩展名:mp4   mime类型:video/mp4,,我本机IIS7-->

<div
id="divVideo"></div>
    <script type="text/javascript">
 
      playVideo({
            //所有参数,elemt和src为必填其他看需求怎么要求
           
//elemt为播放控件要插入的容器,src为视频文件地址,preload为预加载,autoplay是否页面进入就自动播放
           
//poster为播放前的遮照图片,loop为是否循环播放,width和heigth默认100%
            elemt:
document.getElementById("divVideo"),
            src: "3.mp4",  
 //此地址是在本机测试用的,如果发布在服务器上请写服务器的绝对路径。我是发布在外网上的,例如:http://www.tttt.com/test/3.mp4
 
          preload: "true",
            autoplay: "true",
           
poster: "",
            loop: "true",
            width: "",
         
  heigth:""

});

function  playVideo (opt) {
        if (typeof (opt) == "undefined")
{
            alert("请传入必要参数!");
            return;
        }
     
  if (typeof (opt.elemt) == "undefined") {
           
alert("请指定播放器要插入的对象!");
            return;
        }
        if
(typeof (opt.src) == "undefined") {
            alert("请指定要播放视频的路径!");
   
        return;
        }
        var _this = this;
        _this.elemt
= opt.elemt;                                       //播放器要插入的对象
       
_this.src = opt.src;                                          
//视频的URL(必设) 
        _this.width = opt.width > 0 ? opt.width + "px" :
"100%";       //宽度(默认100%) 
        _this.height = opt.height > 0 ?
opt.height + "px" : "100%";    //高度(默认100%)
        _this.autoplay =
opt.autoplay == "true" ? "autoplay" : "";     //自动播放(true为自动播放)
       
_this.poster = opt.poster;                                    
//视频封面,播放时的封面图片
        _this.preload = opt.preload == "true" ? "preload" :
"";        //预加载(true时启动加载)
        _this.loop = opt.loop == "true" ? "loop"
: "";                 //循环播放(true时循环播放)
        var str = "<video
id='playVideo' controls ";                   //根据设置的属性的值,拼写video控件
       
str += " width='" + _this.width + "' height='" + _this.height + "' " +
_this.autoplay + " " + _this.preload + " " + _this.loop + " ";
        if
(typeof (_this.poster) != "undefined") {
            str += " poster='" +
_this.poster + "' >";
        } else {
            str += " >
";
        }
        str += " <source src='" + _this.src + "'
 type='video/mp4; codecs='avc1.42E01E, mp4a.40.2''/>";
        str +=
"</video>";
        _this.elemt.innerHTML = str;                      
            //将str放到要插入的对象中
    }

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