Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

Youtube playlist video starting from 0 instead of playlist.seekTo when logged into Google Account

I am using youtube playlist to add videos and play it in a playlist, and change the start time of the video programmatically. Example: https://jsfiddle.net/s3ow4rv1/

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

var player, playerCont, ini_fullscreen = false, i = 0, ti = [], done = false, ctime = true, play_time = 0, real_play_time;

const playlist = ["QHydkEARVKE"];
const startSeconds_arr = [26];
const endSeconds_arr = [100];

function openFullscreen(elem) {
  if (elem.requestFullscreen) {
    elem.requestFullscreen();
  } else if (elem.mozRequestFullScreen) { /* Firefox */
    elem.mozRequestFullScreen();
  } else if (elem.webkitRequestFullscreen) { /* Chrome, Safari and Opera */
    elem.webkitRequestFullscreen();
  } else if (elem.msRequestFullscreen) { /* IE/Edge */
    elem.msRequestFullscreen();
  }
}

$(document).ready(function() {
    playerCont = $('#video-container')[0];
});

$(window).load(function(){
    player = new YT.Player('ytplayer', {
        width: '100%',
        height: '100%',
        playerVars : {
            playlist: playlist.join(','),
            autoplay: 0, // Auto-play the video on load
            controls: 1, // Show pause/play buttons in player
            modestbranding: 1, // Hide the Youtube Logo
            fs: 1, // Hide the full screen button
            cc_load_policy: 0, // Hide closed captions
            iv_load_policy: 3, // Hide the Video Annotations
            autohide: 0, // Hide video controls when playing
            playsinline: 1, // Plays inline
            rel: 0 // Shows related content from the same channel
        },
        events: {
            onStateChange: function(event) {
                var index = player.getPlaylistIndex();
                if(event.data == YT.PlayerState.BUFFERING){
                    if(!ini_fullscreen){
                        //openFullscreen(playerCont);
                        ini_fullscreen = true;
                    }
                    ctime = false;
                    real_play_time = player.getCurrentTime();
                    if(i != index || (i == playlist.length-1 && typeof ti[i] != 'undefined' && real_play_time == 0)){
                        for(var j = 0;j<ti.length;j++){
                            clearInterval(ti[j]);
                        }
                        i = index;
                        ti = [];
                        player.loadPlaylist(playlist, i,startSeconds_arr[i]);
                        done = true;
                    }
                    else if(!done && i < playlist.length){
                        player.seekTo(startSeconds_arr[i],true);
                        done = true;
                    }
                }
                else if(event.data == YT.PlayerState.PAUSED){
                    ctime = false;
                }
                else if(event.data == YT.PlayerState.PLAYING){
                    if(i < playlist.length){
                        ctime = true;
                        $('#indicate-track').html('Indicator: Video ('+(i+1)+') out of ('+playlist.length+')<br>');
                        real_play_time = player.getCurrentTime();
                        if(play_time != real_play_time){
                            play_time = Math.abs(Math.round(real_play_time - startSeconds_arr[i] - 0.0));
                        }
                        if(typeof ti[i] == 'undefined'){
                            ti[i] = setInterval(function(){
                                if(ctime){
                                    play_time += 0.5;
                                }
                                $('#indicate-time').html('Time Passing: '+Math.round(startSeconds_arr[i]+play_time)+' secs, ends at '+endSeconds_arr[i]+'<br>');
                                if(play_time >= (endSeconds_arr[i]-startSeconds_arr[i])){
                                    clearInterval(ti[i]);
                                    if(i < playlist.length-1){
                                        done = false;
                                        play_time = 0;
                                        player.nextVideo();
                                        i++;
                                    }
                                    else{
                                        done = false;
                                        play_time = 0;
                                        player.stopVideo();
                                        i = 0;
                                    }
                                }
                            }, 500);
                        }
                    }
                }
            }
        }
    });
});

When I am logged into my Google account and try running this script, it always starts at 0. However, when I am logged out of my account or incognito mode, it starts at the time specified in the playlist. I am trying to figure out how being logged in/out of Google account is influencing this script, and anything I can do about it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...