html5中设置或返回音视频是否在加载后即开始播放的属性autoplay

2020-07-26 21:50:53

实例

启用自动播放,并重载视频:

myVid=document.getElementById("video1");myVid.autoplay=true;myVid.load();

定义和用法

autoplay 属性设置或返回音视频是否在加载后即开始播放。

浏览器支持

所有主流浏览器都支持 autoplay 属性。

注释:Internet Explorer 8 或更早的浏览器不支持该属性。

语法

设置 autoplay 属性:

audio|video.autoplay=true|false

返回 autoplay 属性:

audio|video.autoplay

属性值

描述
true指示音视频在加载完成后随即播放。
false默认。指示音视频不应在加载后立即播放。

技术细节

返回值布尔值。true|false
默认值:false

html5 video使用autoplay属性时,声音混乱

页面代码

Index.html

<html xmlns="http://www.w3.org/1999/xhtml"><head><title>test</title> <meta charset='utf-8'/><script src="js/jquery-1.4.4.min.js" type="text/javascript"></script><script src="js/thml5.js" type="text/javascript"></script><script type="text/javascript">window.onload=function(){$('#channel1').click(function(){setConfig("test1.mp4");});$('#channel2').click(function(){setConfig("test2.mp4");});$('#channel3').click(function(){setConfig("test3.mp4");});}function setConfig(url){var jo=$('#test1');var cfg=HTML5MediaService.getDefaultConfig(); cfg=$.extend(cfg, {url: url});HTML5MediaService.create(jo,cfg);}</script></head><body><div id='test1' style="height:300px;width:500px;"></div></br></br></br><div><span id='channel1'>频道1</span><span id='channel2'>频道2</span><span id='channel3'>频道3</span></div></body></html>

js代码
html5.js

var HTML5MediaService= {    getDefaultConfig: function () {        return $.extend({}, {width: "100%", height: "100%", controls: "controls", autoplay: "autoplay"});    },    create:function(jo,cfg){         this.videoId = "videojs_" + new Date().getTime().toString();        var videoJo = $('<video' +            ' id="' + this.videoId + '"' +            ' src=' + cfg.url +            ' controls=' + cfg.controls +            ' autoplay=' + cfg.autoplay +            ' width=' + cfg.width +            ' height=' + cfg.height +            ' preload=none' +            '></video>');        videoJo.appendTo(jo.empty());    }     }

我的解决方案:

取掉autoplay,可以使用play()函数来达到自动播放功能;