chrome音乐html,html - Audio HTML5 autoplay on chrome - Stack Overflow

I had the same problem with videos not autoplaying in Chrome. Chrome now blocks autoplay in video and audio elements. The solution was to delay the .play() function by 1 second. For your audio problem just replace all the 'video' in my snippet with 'audio'.

// Autoplay videos

var playVideos = function () {

// Get all elements with the video tag

var videos = document.getElementsByTagName('video');

// Loop through the videos

for (i = 0; i < videos.length; i++) {

// Play video

videos[i].play();

}

};

// Define body variable

var body = document.getElementById('body');

// Wait 1 Second to trick out Chrome

setTimeout(function() {

// execute the playVideo function

body.addEventListener('onload', playVideos(), false);

}, 1000);

PS: You need to give your Body the id 'body' for this snippet to work.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值