P
P
Petro Boyko2019-02-23 23:48:26
YouTube
Petro Boyko, 2019-02-23 23:48:26

How to script to start Youtube video from a specified time?

there is a working script, but you need to register the start of the video from the specified time, I can’t figure out where to poke it

loadVideoById({'videoId': 'bHQqvYy5KYo',
                   'startSeconds': 5,
                   'endSeconds': 60,
                   'suggestedQuality': 'large'});

        <!DOCTYPE html>
    <html>
    <body>
        <!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
            <div id="player"></div>
    <div class="panel">
    <button id="play" onclick="player.playVideo();">Play</button>
    <button id="pause" onclick="player.pauseVideo();">Pause</button>
    <input type="range" id="plRange" step="2" min="0" max="100">
    </div>      


            <script>
          // 2. This code loads the IFrame Player API code asynchronously.
          var tag = document.createElement('script');

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

          // 3. This function creates an <iframe> (and YouTube player)
          //    after the API code downloads.
          var player;
          function onYouTubeIframeAPIReady() {
            player = new YT.Player('player', {
                height: '360',
                width: '640',
                videoId: 'M7lc1UVf-VE',
                                playerVars: {
                                    'HD': 1,
                                    'modestbranding': 1,
                                    'autoplay': 1,
                                    'controls': 0,
                                    'showinfo': 0,
                                    'rel': 0
                                },
                events: {
                    'onReady': onPlayerReady,
                    'onStateChange': onPlayerStateChange
                }
            });
          }

          // 4. The API will call this function when the video player is ready.
          function onPlayerReady(event) {
            event.target.playVideo();
            plRange.value = player.getVolume();
          }

          // 5. The API calls this function when the player's state changes.
          //    The function indicates that when playing a video (state=1),
          //    the player should play for six seconds and then stop.
          var done = false;
          function onPlayerStateChange(event) {
            if (event.data == YT.PlayerState.PLAYING && !done) {
                setTimeout(stopVideo, 6000);
                done = true;
            }
          }
          function stopVideo() {
            player.stopVideo();
          }
          plRange.oninput = e => player.setVolume(e.target.value);
          document.querySelector('.panel').onwheel = function(e){
    e.preventDefault();
            var del = e.deltaY/50;
            var vol = player.getVolume() + del > 100 ? 100 : player.getVolume() + del < 0 ? 0 : player.getVolume() + del;
            player.setVolume( vol );
            plRange.value = vol;
          };

        </script>
      </body>
      </html>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Petro Boyko, 2019-02-24
@boykopetro

function onPlayerReady(event) {
        player.seekTo(30); // начать воспроизведение с 30-й секунды
        event.target.playVideo();
        plRange.value = player.getVolume();
      }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question