How to stop video after X button is clicked

Viewed 347

Currently, the video does not player.stopVideo(); in the code.

To reproduce: Click 1 svg play button, then click the X and you will see that.

How is that fixed in the code so that the same video stops after clicking the X?

Updated Codes:

Click Run, not update to test JSitor code:

Also, autoplay works inside JSitor: https://jsitor.com/qYKcpdB0tj

Backup copy: https://jsfiddle.net/0q63tr2m/

const videoPlayer = (function makeVideoPlayer() {
  const players = [];

  const tag = document.createElement("script");
  tag.src = "https://www.youtube.com/player_api";
  const firstScriptTag = document.getElementsByTagName("script")[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

  function onPlayerReady(event) {
    const player = event.target;
    player.setVolume(100);
  }

  function addPlayer(video, settings) {
    const defaults = {
      height: 360,
      host: "https://www.youtube-nocookie.com",
      videoId: video.dataset.id,
      width: 640
    };
    defaults.events = {
      "onReady": onPlayerReady
    };

You can see the video still playing here:

enter image description here

3 Answers

What you have to do is as follow:

  1. Add an id to your iframe element like so: id="iframe"
  2. Remove player.stopVideo(); from createStopHandler function
  3. On your createStopHandler function, add following code(if you want your video to stop and not pause, then change the func snippet "func":"pauseVideo" into "func":"stopVideo"):

var frame = document.getElementById("iframe");
frame.contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*');

This should do it. Here you can see it working: https://jsfiddle.net/vjbex6t1/8/

 function addClickToHome(goHome) {
    goHome.forEach(function addEventHandler(goHome) {
        goHome.addEventListener("click", homeClickHandler);
       //STOP PLAYER AFTER HOME IS CLICKED 
    });
}

Initialize some variable with player globally and stop it when button is pressed to home

You can try this out,

var videoURL = $('#playerID').prop('src');
videoURL = videoURL.replace("&autoplay=1", "");
$('#playerID').prop('src','');
$('#playerID').prop('src',videoURL);
Related