Pause or Terminate Youtube Video

Asked

Viewed 866 times

3

Currently I use the following script to pause youtube video:

HTML:

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

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

Javascript inside PHP Repeat 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>

Outside 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>

Completion:

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

inserir a descrição da imagem aqui

1 answer

2

Use your own Javascript API Youtube

Start the video and put it in the player variable (as the example of the API itself)

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

Then just add to your click span the method to stop the video

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

Example using the API Jsfiddle

  • You can make an example here...I could not find this example in the Youtube API, thank you!

  • I realized this tip you passed, but the video only appears for the last movie, which is (WALL). The other does not appear iframe!

  • I will make an example tonight, because here at work youtube is blocked, you could just update html to facilitate?

  • I edited my question there...

  • @user3081follow the example http://jsfiddle.net/kdbgn2qt/

  • Use that version, I found a bug in the previous http://jsfiddle.net/kdbgn2qt/1/

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.