检查Youtube视频ID是否匹配,否则加载新的视频ID

时间:2014-11-24 12:05:09

标签: jquery function iframe youtube-api seek

我想在youtube嵌入视频中寻找,但我有几个视频。

  1. 我在启动时启动了Video-A
  2. 我有一个带2个搜索功能的菜单
  3. 在视频-A上链接搜索
  4. 链接B在Video-B上搜索
  5. 所以我需要检查之前正在播放的视频:
    如果视频ID匹配==>寻求
    否则==>在播放器+搜索中加载新视频(加载和播放新视频时)


    这是我的代码:

    <script type="text/javascript" src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
    <script type='text/javascript' src="https://www.youtube.com/iframe_api"></script>
    <script type='text/javascript'>
        function onYouTubeIframeAPIReady() {
            var player;
            player = new YT.Player('player', {
                videoId: 'JP9-lAYngi4',
                height: '405',
                width: '720',
                playerVars: { 'autoplay': 1},
                events: { 'onStateChange': onPlayerStateChange }
            })
    
            function onPlayerStateChange(event) {        
                if(event.data === 1) {          
                    //alert('playing');
                }
                if (event.data == YT.PlayerState.PLAYING) {
                    var url = event.target.getVideoUrl();
                    // "http://www.youtube.com/watch?v=gzDS-Kfd5XQ&feature=..."
                    var match = url.match(/[?&]v=([^&]+)/);
                    // ["?v=gzDS-Kfd5XQ", "gzDS-Kfd5XQ"]
                    var videoId = match[1];
                    //alert(videoId)
    
                    $('#linkok').click(function(){ 
                        if(videoId == "JP9-lAYngi4") {
                            //alert("same as onload:" + videoId);
                            player.loadVideoById("x9Us_jMV8hA");
                            //alert("new video loaded: x9Us_jMV8hA")
                        } 
                        else {
                            alert("not the same:" + videoId)
                        }                           
                    });
                };
            };
        };
    </script>
    

    我的视频div和菜单:

    <div id="video"></div>
    <a href="javascript:;" onClick="player.playVideo();player.seekTo(60);">ON CLICK SEEKTO 60s in Video-A</a><br>
    <a href="javascript:;" onClick="player.playVideo();player.seekTo(120);">ON CLICK SEEKTO 120s in Video-B</a><br>
    

    我的代码做得不好,并没有像我期待的那样真正地工作 你能帮我改善一下吗?

    提前致谢

1 个答案:

答案 0 :(得分:0)

我设法这样做了,但请告诉我如何简化此代码:

<script type='text/javascript'>
    var videoId;
    function onYouTubeIframeAPIReady() {
        var player;
        player = new YT.Player('player', {
            videoId: 'joITmEr4SjY',
            height: '405',
            width: '720',
            playerVars: { 'autoplay': 1},
            events: { 'onStateChange': onPlayerStateChange }
        });

        function onPlayerStateChange(event) {        
            if(event.data === 1) {          
                //alert('playing');
            }
            if (event.data == YT.PlayerState.PLAYING) {
                var url = event.target.getVideoUrl();
                // "http://www.youtube.com/watch?v=gzDS-Kfd5XQ&feature=..."
                var match = url.match(/[?&]v=([^&]+)/);
                // ["?v=gzDS-Kfd5XQ", "gzDS-Kfd5XQ"]
                videoId = match[1];
            }

        };
        $('#link01').click(function(){ 
            if(videoId == "joITmEr4SjY") {
                alert("Good Video: " + videoId);
                player.seekTo(120);
            }
            else {
                alert("Wrong Video: " + videoId)
                player.loadVideoById("joITmEr4SjY");
                player.seekTo(120);
            }                           
        });
        $('#link02').click(function(){ 
            if(videoId == "l9dpjN3Mwps") {
                alert("Good Video: " + videoId);
                player.seekTo(240);
            }
            else {
                alert("Wrong Video: " + videoId)
                player.loadVideoById("l9dpjN3Mwps");
                player.seekTo(240);
            }                           
        });
    };
</script>