寫(xiě)了個(gè)html5播放視頻的video控件,只支持mp4和3gp(android和ios默認(rèn)支持的格式就寫(xiě)了這個(gè)) ,需要的朋友可以參考下
代碼如下:
<!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 id="divVideo"></div>
//因js水平有限,不喜勿噴,全當(dāng)沒(méi)事看看,video是html5中的新控件,大家可以看看
<script type="text/javascript">
//mp4是ios、android普遍支持的格式
function playVideo(opt) {
if (typeof (opt) == "undefined") {
alert("請(qǐng)傳入必要參數(shù)!");
return;
}
if (typeof (opt.elemt) == "undefined") {
alert("請(qǐng)指定播放器要插入的對(duì)象!");
return;
}
if (typeof (opt.src) == "undefined") {
alert("請(qǐng)指定要播放視頻的路徑!");
return;
}
var _this = this;
_this.elemt = opt.elemt; //播放器要插入的對(duì)象
_this.src = opt.src; //視頻的URL(必設(shè))
_this.width = opt.width > 0 ? opt.width + "px" : "100%"; //寬度(默認(rèn)100%)
_this.height = opt.height > 0 ? opt.height + "px" : "100%"; //高度(默認(rèn)100%)
_this.autoplay = opt.autoplay == "true" ? "autoplay" : ""; //自動(dòng)播放(true為自動(dòng)播放)
_this.poster = opt.poster; //視頻封面,播放時(shí)的封面圖片
_this.preload = opt.preload == "true" ? "preload" : ""; //預(yù)加載(true時(shí)啟動(dòng)加載)
_this.loop = opt.loop == "true" ? "loop" : ""; //循環(huán)播放(true時(shí)循環(huán)播放)
var str = "<video id='playVideo' controls "; //根據(jù)設(shè)置的屬性的值,拼寫(xiě)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 + "' />";
str += "</video>";
this.elemt.innerHTML = str; //將str放到要插入的對(duì)象中
}
playVideo({
//所有參數(shù),elemt和src為必填其他看需求怎么要求
//elemt為播放控件要插入的容器,src為視頻文件地址,preload為預(yù)加載,autoplay是否頁(yè)面進(jìn)入就自動(dòng)播放
//poster為播放前的遮照?qǐng)D片,loop為是否循環(huán)播放,width和heigth默認(rèn)100%
elemt: document.getElementById("divVideo"),
src: "3.mp4",
preload: "true",
autoplay: "true",
poster: "",
loop: "true",
width: "",
heigth:""
});
</script>
</body>
</html>
更多信息請(qǐng)查看IT技術(shù)專欄