javascript - Cannot autoplay YouTube on mobile devices - Stack Overflow

So I cannot autoplay a YouTube video due to this update which prevents videos that aren't muted fr

So I cannot autoplay a YouTube video due to this update which prevents videos that aren't muted from playing on mobile:

I've tried setting mute=1 and volume=0 in my Video URL. The mute parameters does successfully mute the video on Desktop, but I still cannot get the video to autoplay on mobile. The reason I need autoplay to work is because I am loading an image of the video on the initial page load. Once the user clicks the video, my JavaScript loads the iframe. This significantly helps the performance of my page. There are several other questions on Stack Overflow that are related but none of the answers have worked.

Here is my current code:

<iframe width="560" height="315" src=";mute=1" volume="0" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

So I cannot autoplay a YouTube video due to this update which prevents videos that aren't muted from playing on mobile: https://developers.google./web/updates/2017/09/autoplay-policy-changes

I've tried setting mute=1 and volume=0 in my Video URL. The mute parameters does successfully mute the video on Desktop, but I still cannot get the video to autoplay on mobile. The reason I need autoplay to work is because I am loading an image of the video on the initial page load. Once the user clicks the video, my JavaScript loads the iframe. This significantly helps the performance of my page. There are several other questions on Stack Overflow that are related but none of the answers have worked.

Here is my current code:

<iframe width="560" height="315" src="https://www.youtube./embed/QHfgMrpMOm4?autoplay=1&mute=1" volume="0" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
Share Improve this question asked Feb 5, 2019 at 0:04 Cannon MoyerCannon Moyer 3,1844 gold badges37 silver badges83 bronze badges 1
  • 2 you can check this answer in ios the autoplay not going to work without the user interaction. stackoverflow./questions/15090782/… – Chilll007 Commented Feb 5, 2019 at 7:17
Add a ment  | 

1 Answer 1

Reset to default 3

Had same issue, looks like something changed. Only solution, that worked for me was - use youtube API

var tag = document.createElement('script');

tag.src = "https://www.youtube./iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;

function onYouTubeIframeAPIReady() {
  player = new YT.Player('player', {
    videoId: 'M7lc1UVf-VE',
    events: {
      'onReady': onPlayerReady,
    }
  });
}

function onPlayerReady(event) {
  event.target.mute();
  event.target.playVideo();
}
<!DOCTYPE html>
<html>

<body>
  <div id="player"></div>
</body>

</html>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745447871a4628129.html

相关推荐

  • javascript - Cannot autoplay YouTube on mobile devices - Stack Overflow

    So I cannot autoplay a YouTube video due to this update which prevents videos that aren't muted fr

    6小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信