如何在具有多个视频的页面中播放其他视频时暂停YouTube视频

时间:2014-09-16 15:41:03

标签: javascript youtube-api

我在一个页面中有6个视频,这些视频是使用SWFOBJECT API创建的。     要求是在播放另一个视频时暂停视频     例如:视频的swfobject是

<DIV id=ytcontainer></DIV>
<SCRIPT>  
 swfobject.embedSWF("http://www.youtube.com/v/erDxb4IkgjM?version=3&f=user_uploads&app=youtube_gdatafs=1&rel=0&enablejsapi=1&playerapiid=myytplayer", "ytcontainer", "660", "394", "8", null, null, {allowscriptaccess:"always",allowfullscreen:"true",wmode:"opaque"}, {id:"myytplayer"});
</SCRIPT>
like this way, i have 6 videos in a single page

下面是暂停视频的代码

<script>
swfobject.addLoadEvent(onYouTubePlayerReady);
            var status;
            var videostate2=0;  var videostate1=0;
           function onYouTubePlayerReady(playerId)
            {
                var myPlayer1 = document.getElementById('myytplayer');
                var myPlayer2 = document.getElementById('qualityvideotrack');
                myPlayer1.addEventListener("onStateChange", "onytplayerStateChange1");
                myPlayer2.addEventListener("onStateChange", "onytplayerStateChange2");
            }

            function onytplayerStateChange1(newState)
            {
                   if(newState1==1)
                   {
                    videostate1=1;
                     pause();
                     }
                    else
                    videostate1=0;


                 return videostate1;
            }

            function pause()
            {
                var state;
                console.log("pause");
                var myPlayer2 = document.getElementById('qualityvideotrack');
                var myPlayer1 = document.getElementById('myytplayer');
                if(videostate2==1)
                {
                state=onytplayerStateChange2(videostate2);
                console.log("Player's new state:2 before pause "+state);
                if(state==1)
                myPlayer2.pauseVideo();
                }
                if(videostate1==1)
                {
                state=onytplayerStateChange1(videostate1);
                console.log("Player's new state:1 before pause "+state);
                if(state==1)
                myPlayer1.pauseVideo();
                }

            }

            function onytplayerStateChange2(newState2)
            {
            if(newState2==1)
            {
            videostate2=1;
            pause();
            }
            else
            videostate2=0;

        return videostate2;

            }

</script>

上述代码无法正常运行。但是,如果我编写代码来停止一个视频而其他视频正在播放,则反之亦然。请建议我。

0 个答案:

没有答案