html5-声音视频自动播放的坑

野生程序猿-杂烧5年前随意分享1085

因为从事的是教学软件的开发,不知何年何月开始声音、视频不支持自动播放了。必须点一个按钮才能播放。教学类软件很多都是有动画的,像播放器一样,从头开始慢慢播放,可以理解为n帧,但是因为程序开发不是视频制作,没有序列帧,所以要通过回调实现这些功能。经常开发这类软件,得寻找自己的方案。


果断选择了jquery,没错就是古老的jquery。直接对dom进行监听。哪个元素不能自动播放,我就在哪个元素上插个必须点击才能播放。

部分代码片段

var this_v=$(`#video_${this_id}`);
					
this_v.attr("src",this_v.attr("xsrc"));

var this_v_dom=document.getElementById(`video_${this_id}`); 
var this_c_dom=document.getElementById(`canvas_video_${this_id}`); 

this_c_dom.width = 1920;
this_c_dom.height = 1080;
var this_ctx=this_c_dom.getContext('2d');

var captureFrame=function() {
	// console.error('captureFrame');
	// console.error(this_v_dom);
	// console.error(this_ctx);
	this_ctx.drawImage(this_v_dom, 0, 0, this_c_dom.width, this_c_dom.height);
	setTimeout(function(){
		captureFrame();
	},40);
}

try{
	this_v_dom.play();
}catch(e){}

var this_played=false;

this_v_dom.addEventListener("ended",function(){
		 console.error("播放结束");
		 go_next();
})
this_v_dom.addEventListener("play",function(){
		 this_played=true;
		 console.error('自动播放了');
		 captureFrame();
})

setTimeout(function(){
	console.error('this_played',this_played);
	if(this_played==false)
	{
		//this_v_dom.muted=true;
		
		var dianji_html=`<div id="dianjipingmu">点击屏幕继续</div>`;
		$("#dianjipingmu").remove();
		$(".app").eq(0).prepend(dianji_html);
		$("#dianjipingmu").click(function(){
			$("#dianjipingmu").remove();
			this_v.attr("src",this_v.attr("xsrc"));
			this_v_dom.play();
		});
	}
	
},588);

野生办法,延迟判断一下是否播放成功。并监听是否播放结束,然后go_next();

相关文章

编译新的js库用在传统模式直接使用

面向dom的jquery被淘汰了,但是jquery拥有多年的生态,很多时候还是可以用在一些项目上的。没有最好的开发语言,也没有最好的框架;只有最适用的开发语言、最适用的框架。很多时候我们敏捷开发者都会...

推荐一个前端框架Svelte(超级轻量级)

现在的前端框架太多了,国内用得比较多的是vue,非常强大,但是个人感觉灵活性糟糕,代码比较繁琐。近期接触一个框架,顿时让我感觉非常的爽,一种似曾相似的感觉,这个框架就是Svelte。Svelte是一个...

nodejs简单实现端口映射(隧道)

nodejs简单实现端口映射(隧道)

来个万能的javascript代码,实现端口映射。前端electron桌面软件也可以用到。【port.js】var net = require('net'...

编译的困扰-经验分享

编译的困扰-经验分享

开发的时候我们编译经常遇到坑,一会要用python3,一会又要用python 2.7的情况,nodejs一会要这个版本,一会要那个版本的情况,甚至npm都有版本要求。...

python RabbitMq的订阅

【test_RabbitMq1.py】import pika import json credentials = pika.PlainCredentia...

vue编译成微信小程序的坑

uniapp最大的好处是方便,最大的问题就是编译后多端的异常难以排查。h5端一切正常,在其他端异常。代码不存在语法上的错误,几乎都是平台的差异性导致的,这个时候多半是经验积累不够,坑踩得不够多。很多时...