HTML5高阶实例之video
2018-03-27 18:13
246 查看
更新日期:2018年3月27日18:07:55
动态创建常规video
动态创建id不重复的可控video
动态创建常规video
let userVideo = document.createElement('video'); document.body.appendChild(userVideo); //属性width height autoplay id type src,也可以通过userVideo.setAttribute('type','video/mp4');来设置 userVideo.width = 320; userVideo.height = 240; userVideo.autoplay = true; userVideo.id = 'userVideo-2'; userVideo.type = 'video/mp4'; userVideo.src = "http://60.210.16.52/videos/other/1.mp4";//浏览器只支持mp4 hls //userVideo.load(); //userVideo.play(); userVideo.oncanplaythrough = document.getElementById("userVideo-1").play();
动态创建id不重复的可控video
function createVideo(id,src,autoplay,controls){ //判断id是否重名 if(duplicationId(id)){ console.log('id 重名,无法创建!'); return null; } //创建video let customVideo = document.createElement('div');// customVideo.id = id; customVideo.src = src; customVideo.autoplay = autoplay; customVideo.controls = controls; customVideo.type = "video/mp4"; return customVideo } function duplicationId(id){ let allEle = document.getElementsByTagName('video'); let idArr = []; for(let i=0,len=allEle.length;i<len;i++){ idArr.push(allEle[i].getAttribute('id')); } if(idArr.indexOf(id) !== -1){ console.log('duplication id') return true }else{ console.log('can set id') return false } } //添加video let a = createVideo('customVideo1',"http://60.210.16.52/videos/other/20180228/ee/6e/dd04b4c964066489cd90 4000 c04fe79a1339.mp4?dis_k=27b2ad2afe70203668d7ac0c1df14054a&dis_t=1522138750&dis_dz=CNC-QiYi&dis_st=46&src=iqiyi.com&uuid=ca6c0ef0-5ab9fe7e-89&z=zibo5_cnc&pv=0.2",true,true); if(a){ document.body.appendChild(a); a.addEventListener('play',function(){ console.log(a.getAttribute('id') + ' play'); }); //音频/视频已暂停 a.addEventListener('pause',function(){ console.log(a.getAttribute('id') + ' pause'); }); //播放结束 a.addEventListener('ended',function(){ console.log(a.getAttribute('id') + ' end'); }); } let b = createVideo('customVideo2',"http://60.210.16.52/videos/other/20180228/ee/6e/dd04b4c964066489cd90c04fe79a1339.mp4?dis_k=27b2ad2afe70203668d7ac0c1df14054a&dis_t=1522138750&dis_dz=CNC-QiYi&dis_st=46&src=iqiyi.com&uuid=ca6c0ef0-5ab9fe7e-89&z=zibo5_cnc&pv=0.2",true,true); if(b){ document.body.appendChild(b); //音频/视频已开始或不再暂停 b.addEventListener('play',function(){ console.log(b.getAttribute('id') + ' play'); }); //音频/视频已暂停 b.addEventListener('pause',function(){ console.log(b.getAttribute('id') + ' pause'); }); //播放结束 b.addEventListener('ended',function(){ console.log(b.getAttribute('id') + ' end'); }); }
相关文章推荐
- transform实现HTML5 video标签视频比例拉伸实例详解
- 【实例】HTML5中video播放视频实现特效
- HTML5 Video
- HTML5无插件多媒体Media——音频audio与视频video
- 微信网页开发之video标签[HTML5微信播放器video]
- 自定义基于HTML5的video播放器—Customize your video player
- HTML5 Audio/Video 标签属性与事件
- html5-表单的综合实例
- 使用HTML5的video标签播放视频
- android webkit HTML5 video研究
- html-----vedio标签(HTML5新标签VIDEO在IOS上默认全屏播放)
- JavaScript、jQuery、HTML5、Node.js实例大全-读书笔记3
- 关于html5的video全屏作为背景的方法
- 使用js检测浏览器是否支持html5中的video标签的方法
- html5新元素之video,audio,meter,datalist,keygen,output
- HTML5 VIDEO标签播放事件流水
- HTML5新标签video在iOS上默认全屏播放
- 使用HTML5抓取 Audio & Video
- 基于HTML5的播放器Sublimevideo走免费增值路线
- HTML5 video 视频标签使用介绍