Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

javascript - stop all videos when a new video is played

I need some help with Youtube API and embeded videos. I want to stop all iframe videos (here i have taken only 3, but there are several videos)running on the current page when a new youtube video is clicked. At one point of time, only one iframe youtube video should run. I have gone thruogh documentation [https://developers.google.com/youtube/js_api_reference][1] and was able to write till here...

Updated:

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="http://www.youtube.com/player_api"></script>
        <script type="text/javascript">
            var player;
             function onYouTubeIframeAPIReady() {
                player = new YT.Player('player', {
                    events: {
                        'onReady': onPlayerReady,
                        'onStateChange': onPlayerStateChange
                    }
                });
            }

            var done = false;
              function onPlayerStateChange(event) {
                if (event.data == YT.PlayerState.PLAYING && !done) {
                    alert("hi5");
                    player1.stopVideo();
                    player2.stopVideo();
                    done = true;
                }
               }
        </script>
    </head>
    <body>
        <div>TODO write content</div>
        <ul class="image-grid" id="list"> 
            <li>
                <iframe id="player" width="385" height="230" src="http://www.youtube.com/embed/erDxb4IkgjM?rel=0&wmode=Opaque&enablejsapi=1;showinfo=0;controls=0" frameborder="0" allowfullscreen></iframe> 
            </li>
            <li>
                <iframe id="player1" width="385" height="230" src="http://www.youtube.com/embed/wSrA5iQGlDc?rel=0&wmode=Opaque&enablejsapi=1;showinfo=0;controls=0" frameborder="0" allowfullscreen></iframe>
            </li>
            <li>
                <iframe id="player2" width="385" height="230" src="http://www.youtube.com/embed/c7b_WLkztXc?rel=0&wmode=Opaque&enablejsapi=1;showinfo=0;controls=0" frameborder="0" allowfullscreen></iframe>
            </li>
        </ul>
    </body>
</html>

UPDATED

  1. onStateChange event is not getting triggered(working)
  2. In this example i have 3 videos, in reality it has more videos instead of writing onPlayerStateChange for each video..is it possible to use an array for all the videos..and in the function onPlayerStateChange, to write player[].stopVideo() and this.playvideo()..something of this sort..(help required) please see this fiddle http://jsfiddle.net/LakshmiV/kn5Sf/ Please help.
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

You can do something like this...

Give every iframe a class so that it can be identified as an iframe for youtube player. Here I have given "yt_players"

Now you can use the below code...

<script type="text/javascript">
    players = new Array();

    function onYouTubeIframeAPIReady() {
        var temp = $("iframe.yt_players");
        for (var i = 0; i < temp.length; i++) {
            var t = new YT.Player($(temp[i]).attr('id'), {
                events: {
                    'onStateChange': onPlayerStateChange
                }
            });
            players.push(t);
        }
    }
    onYouTubeIframeAPIReady();

    function onPlayerStateChange(event) {
        if (event.data == YT.PlayerState.PLAYING) {
            var temp = event.target.a.src;
            var tempPlayers = $("iframe.yt_players");
            for (var i = 0; i < players.length; i++) {
                if (players[i].a.src != temp) 
                    players[i].stopVideo();
            }
        }
    }
</script>

Have updated the code...This should be of help to you.

See it in here...http://jsfiddle.net/anubhavranjan/Y8P7y/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...