loadVideoById()不会更改youtube播放器中的视频



我正在使用Iframe YouTube API开发YouTube播放器。播放器加载视频,但当我尝试使用onclick事件更改视频时,它不会更改视频。这是我的HTML。

<li onclick="changeVideo('Vw4KVoEVcr0')"><a href=""><img class="button" src="" alt="Video Button"> item</a></li>

这是我的JavaScript。

// JavaScript Document
//create script tag for youtube api
var tag = document.createElement("script");
//set the src attribute of the script tag to the youtube api
tag.src = "https://www.youtube.com/iframe_api";
//get the first script tag of the document
var firstScript = document.getElementsByTagName("script")[0];
//insert our youtube api tag before the first script tag in the page
firstScript.parentNode.insertBefore(tag, firstScript);
/*
*
*
*/
//the video player element
var player;
//state of the video being played
var done;
/*
*
*/
function onYouTubeIframeAPIReady(){
    //
    player = new YT.Player("player", {
        width: "848",
        height: "400",
        videoId: "Unp7GtSPJ0Y",
        events: {
            "onReady": onPlayerReady,
            "onStateChange": onPlayerStateChange
        }
    });
}
/*
*
*/
function onPlayerReady(event){
    event.target.playVideo();   
}
/*
*
*/
function onPlayerStateChange(event){
    if(event.data == YT.PlayerState.PLAYING && !done){
        setTimeout(stopVideo, 6000);
        done = true;
    }
}
/*
*
*/
function stopVideo(){
    player.stopVideo();
}
/*
* Changes the videoId and starts playing the new video
*/
function changeVideo(videoIdentifier){
    var vidId = videoIdentifier;
    player.loadVideoById(vidId);
}

只是我睡了很小的觉后很愚蠢,哈哈。去掉链接。在视频更改之前,它不断将我重定向到页面的新版本。

相关内容

  • 没有找到相关文章

最新更新