Pause or Shut down youtube video

3

I currently use the following script to pause the youtube video:

HTML:

<span onClick="stopVideo();" class="wmg-close"></span>

<div id="player<?= $dAgenda['id'];?>"></div>

JavaScript within the PHP Replication Loop:

<script type="text/javascript">   
            var player;
              function onYouTubeIframeAPIReady() {
                player = new YT.Player('player<?= $dAgenda['id'];?>', {
                  height: '230',
                  width: '454',
                  videoId: '<?= $dAgenda['link'];?>',
                  events: {}
                });
            }
              function stopVideo() {
                player.stopVideo();
              }         
            </script>

Out of PHP Repeat Loop:

<script type="text/javascript">
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');

tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
</script>

Conclusion:

The video is only appearing for the last movie in the PHP loop!

    
asked by anonymous 18.09.2015 / 22:17

1 answer

2

Use your own JavaScript API YouTube

Start the video and put it in the player variable (according to the API's own example)

var player;
function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
      height: '390',
      width: '640',
      videoId: 'M7lc1UVf-VE',
      events: { }
    });
}

Then just add the method to stop the video to the% of your%

function stopVideo() {
    player.stopVideo();
}

Example using the JSFiddle API

    
18.09.2015 / 22:30