Skip to content Skip to sidebar Skip to footer

Youtube Api Detect If Autoplay Is Disabled

I have used Youtube API to play automatically a video but i have found out that it only works fine on PCs but NOT mobile devices. I did a bit of google-ing and discovered that auto

Solution 1:

It appears that there is no way of getting this information without testing with you video and checking whether the video's current time has advanced after it has loaded.

It was asked for as a feature in modernizr but as you can see from this comment from Paul Irish, you have to have a video to check. It looks like they are trying to add this into v3 of modernizr.

So essentially it is doable and his pseudocode should get you started. Basically the first four points are done already. You would just need to implement the last two.

create avideo element
set video element srcto something
add autoplay attribute to it
add element to the dom
bind to that event (something metadata soemthing) that fires when its buffered some
setTimeout after it fires and see if currentTime is > 0

Solution 2:

A sample to autoplay without using arg autoplay. I only test this solution on PCs not mobile but you can try this code :

LIVE EXAMPLE

HTML

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

JS

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

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


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

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


    var player;
          functiononYouTubeIframeAPIReady() {
            player = newYT.Player('player', {
              height: '390',
              width: '640',
              videoId: 'M7lc1UVf-VE',
              events: {
                'onReady': onPlayerReady,
                'onStateChange': onPlayerStateChange
              }
            });
          }


          functiononPlayerReady(event) {
            event.target.playVideo();
          }

          functiononPlayerStateChange(event) {
            if (event.data == YT.PlayerState.PLAYING) {

            } else {

            }
          }
    functionstopVideo() {
            player.stopVideo();
          }

Post a Comment for "Youtube Api Detect If Autoplay Is Disabled"