Stop YouTube Video from Playing when FlexSlider Slide is out of view

f1ss1on

I am trying to put some YouTube embedded videos into a FlexSlider Carousel / Slider. The issue I am having is to have the video stop if the slide is not in view. Currently, if you play the video and select a new video, the previous one keeps playing.

Expected: On slide change, stop the video. Actual: On slide change, the video keeps playing.

HTML:

<section class="videolist">
  <div class="container">
    <div id="slider" class="flexslider">
      <ul class="slides">
        <li>
          <iframe width="560" height="315" src="https://www.youtube.com/embed/xcJtL7QggTI" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
        </li>
        <li><iframe width="560" height="315" src="https://www.youtube.com/embed/a3ICNMQW7Ok" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
        </li>
        <li> <iframe width="560" height="315" src="https://www.youtube.com/embed/S-thTTqefls" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> </li>
        <li> <iframe width="560" height="315" src="https://www.youtube.com/embed/Bey4XXJAqS8" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> </li>

      </ul>
    </div>
    <div id="carousel" class="flexslider">
      <ul class="slides">
        <li> <img src="https://quidsup.net/wallpaper/abstract/the-cube-1920x1080-wallpaper-5189.jpg" alt="This is the alt text of the video"> </li>
        <li> <img src="https://www.setaswall.com/wp-content/uploads/2017/08/Spaceship-Background-20-1920x1080.jpg" alt="This is the alt text of the video"> </li>
        <li> <img src="https://wallpapercave.com/wp/trlzN6b.jpg" alt="This is the alt text of the video"> </li>
        <li> <img src="https://i.redd.it/vjcavsc1lcs11.jpg" alt="This is the alt text of the video"> </li>

      </ul>
    </div>
  </div>
</section>

JS:

$(window).load(function() {  
  $('#carousel').flexslider({
    animation: "slide",
    controlNav: false,
    animationLoop: false,
    slideshow: false,
    itemWidth: 210,
    itemMargin: 5,
    asNavFor: '#slider'
  }); 
  $('#slider').flexslider({
    animation: "slide",
    controlNav: false,
    animationLoop: false,
    slideshow: false,
    sync: "#carousel"
  });
});

I have a codepen up here YouTube FlexSlider Carousel | Codepen

Any ideas how to accomplish this?

Thanks

f1ss1on

With help, it was figured out by adding in some js.

$(window).load(function() {  
  $('#carousel').flexslider({
    animation: "slide",
    controlNav: false,
    animationLoop: false,
    slideshow: false,
    itemWidth: 210,
    itemMargin: 5,
    asNavFor: '#slider',
    start: function() {
      $('#carousel li').on('click', function(e){
        stopVideos();
      });
      $('#carousel li').on('click', function(e){
        stopVideos();
      });
    }
  }); 
  $('#slider').flexslider({
    animation: "slide",
    controlNav: false,
    animationLoop: false,
    slideshow: false,
    sync: "#carousel",
    start: function() {
      $('.flex-next').on('click', function(e){
        stopVideos();
      });
      $('.flex-prev').on('click', function(e){
        stopVideos();
      });
    }
  });
});

function stopVideos() {
  $('.iframe-vid').each(function() {
  this.contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*')
  });
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to detect when a youtube video finishes playing?

Youtube Video Still Playing When Bootstrap Modal Closes

Stop playing video in iframe when modal is closed

Stop the video from playing

Pause a currently playing YouTube video when a new video slide is clicked in flex slider angularjs

Youtube video stop when I close popup

Pause Bootstrap carousel when playing Youtube video

Stop exo player video from playing when exit from activity android

java.lang.NoClassDefFoundError occured when playing video youtube in activity

How to stop a youtube video from playing

How can I stop an embedded iframe youtube video from playing when a modal is closed?

Stop Playing a Video from QMediaPlayer at Position X

React native webview crash when playing youtube embed video

Stop the current video playing when user play another video?

How to stop YouTube video when hidding a Div

Playing video from youtube link without UIWebView

How to stop html5 video from playing when out of viewport?

iOS - Playing a YouTube video in fullscreen from a table cell

Jquery make Youtube video stop playing when clicking outside popup

Detecting when a YouTube video has finished playing

Getting "Time Elapsed" of a playing video from the YouTube API in ActionScript 3.0

How to stop a flex video from playing on load

Slide down to a div when Youtube video Play ends

AngularJS and Youtube, call a script when video finishes playing

Stop youtube video playing after tab exit

How to tell when youtube video stops playing from the terminal? macOS

Stop the current video from playing when the next button is clicked in Drupal 7

How to stop the video player from playing when moving from one page to another

stop video from auto playing swift UI