开发者

While using the YouTube api, the loadvideo function wont work unless I alert the video id first

开发者 https://www.devze.com 2023-01-30 09:04 出处:网络
function playPlaylist(trackstemp) { trackstemp = trackstemp.split(\' \'); for (i=0; i < trackstemp.length; i++){
function playPlaylist(trackstemp) {
    trackstemp = trackstemp.split(' ');
    for (i=0; i < trackstemp.length; i++){
        tracks[i] = trackstemp[i];
    }
    numoftracks = tracks.length - 1;
    currenttrack = 0;
    loadNewVideo(tracks[currenttrack])
}

function loadNewVideo(id) {
ytplayerid.loadVideoById(id, 0);
}

I have a prev() and next() function that work just fine by calling loadNewVi开发者_运维问答deo(tracks[currenttrack]), but the initial video only works if I alert(id) within the loadNewVideo() function.

Any clue to why this is happening?


Are you using onYouTubePlayerReady which tells you when the player is ready?

function onYouTubePlayerReady(){
    //Call you first track here
}


Mostly likely the effect of the alert is to delay the execution of ytplayerid.loadVideoById(id, 0); so that it satisfies a race condition in your code - for example, the function doesn't exist yet, or some dependency in the function is not yet settled.

Make sure that the first loadNewVideo happens after all other scripts are loaded and the DOM is ready (ie, by attaching the function to the window.load event).


Separate this out and put in that head in its own script tags.

<script type="text/javascript" language="javascript">
  function onYouTubePlayerReady(playerid) { 
    ytp = document.getElementById('ytplayer');      
    ytp.mute();
  };
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消