0
votes

I am looking to have a slide in a slideshow that contains a YouTube video. It should:

  • pause on mouseover
  • pause during playing a YouTube video
  • contain pagination

I used the JQuery Cycle Plugin for the slideshow. http://jquery.malsup.com/cycle/

and used YouTube's iframe API https://developers.google.com/youtube/iframe_api_reference

The slideshow pauses and resumes correctly for play/pause of the video; however, if you mouseout of the video slide it will resume.

Is there any way to prevent this using the JQuery Cycle Plugin?

Thanks.

<!-- added to example from http://jquery.malsup.com/cycle/ -->

<html>
<head>
<title>JQuery Cycle Plugin - with YouTube</title>

<style type="text/css">
.slideshow { height: 232px; width: 232px; margin: auto }
.slideshow img { padding: 15px; border: 1px solid #ccc; background-color: #eee; }
/* slideshow pagination */
#nav_slideshow { width: 232px; margin: auto }
#nav_slideshow a { border: 1px solid #ccc; background: #fc0; text-decoration: none; margin: 0 5px; padding: 3px 5px;  }
#nav_slideshow a.activeSlide { background: #ea0 }
#nav_slideshow a:focus { outline: none; }
</style>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
    <script type="text/javascript" src="http://cloud.github.com/downloads/malsup/cycle/jquery.cycle.all.latest.js"></script>
    <script type="text/javascript">

      var theslideshow;
      $(document).ready(function() {
        theslideshow = $(".slideshow")
            .before('<div id="nav_slideshow">')
            .cycle({
                fx: 'fade',
                pause: true,  // pause on mouseover - will still fire resume 
                              // after cycle('pause') is called to play video 
                              // then if user leaves YouTube's iframe it resumes slideshow
                timeout: 2500,
                speed: 2500,
                sync: 1,
                pager:  '#nav_slideshow'
            });
      }); 

      // Access YouTube's APIs
      // https://developers.google.com/youtube/iframe_api_reference
      var tag = document.createElement('script');
      tag.src = "https://www.youtube.com/iframe_api";
      var firstScriptTag = document.getElementsByTagName('script')[0];
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

      var player;
      function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
          events: {
            'onStateChange': onPlayerStateChange
          }
        });
      }

      // pause slide show when youtube video is playing <-- not working yet!
      // mouseout event triggers resume in Cycle's pause on mouseover
      function onPlayerStateChange(event){
        console.log(event.data);
        if(event.data == '1') {
            theslideshow.cycle('pause');
        } else if((event.data == '0') || (event.data == '2')) {
            theslideshow.cycle('resume');
        }
      }

    </script>
    </head>
    <body>
        <div class="slideshow">
            <img src="http://cloud.github.com/downloads/malsup/cycle/beach1.jpg" width="200" height="200" />
            <img src="http://cloud.github.com/downloads/malsup/cycle/beach2.jpg" width="200" height="200" />
            <img src="http://cloud.github.com/downloads/malsup/cycle/beach3.jpg" width="200" height="200" />
            <img src="http://cloud.github.com/downloads/malsup/cycle/beach4.jpg" width="200" height="200" />
            <img src="http://cloud.github.com/downloads/malsup/cycle/beach5.jpg" width="200" height="200" />
            <iframe id="player" width="560" height="315" src="http://www.youtube.com/embed/7CGQzQuH4X4?enablejsapi=1" frameborder="0" allowfullscreen></iframe>
        </div>
    </body>
    </html>
1

1 Answers

0
votes

Not a solution to exact problem - but a simple alternative.

Since you use cycle - upgrade to cycle2 - http://jquery.malsup.com/cycle2/ - there is already a cycle2 extension for youtube videos (check download section) .

You can use 90% of your code, since the syntax is mostly the same - except some new options (and few are renamed).