Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop video from playing when tab is inactive #8

Closed
cferdinandi opened this issue Feb 7, 2014 · 6 comments
Closed

Stop video from playing when tab is inactive #8

cferdinandi opened this issue Feb 7, 2014 · 6 comments

Comments

@cferdinandi
Copy link
Owner

No description provided.

cferdinandi pushed a commit that referenced this issue Feb 17, 2014
@cferdinandi
Copy link
Owner Author

Added in version 5.3

@elalemanyo
Copy link

Hi,
The way you stop the videos is making them reload it again when the user get back to the tab.
I have a small js to pause the video on tab change event, is it possible to make stopVideos a setting?

Thanks!

@cferdinandi
Copy link
Owner Author

@elalemanyo It's possible, but I'm not sure it's worth the extra conditional to do so. Would you mind sharing your code so I can see what you're doing?

Worth mentioning, the reload only happens with iFrames. HTML5 video is paused using the video API.

@elalemanyo
Copy link

I have some youtube iframe and I also have some other iframes. All the time was reloading all of them, when user change the tab, that was really annoying.

Also I had a small code to pause youtube videos, using youtube API.

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 players = {};
window.onYouTubePlayerAPIReady = function() {
  $('.tabs-content #videos').controlYTiframes();

  $('.tabs-content #videos').on('activeTab', function() {
    $.each(players, function(index, val) {
      stopVideo(index);
    });
   });
};

$.fn.controlYTiframes = function() {
  $(this).find('iframe').each(function (k, v) {
    players[v.id] = new YT.Player(v.id, {
      events: {
        'onStateChange': onPlayerStateChange
      }
    });
  });
}

function onPlayerStateChange(event) {
  if (event.data == YT.PlayerState.PLAYING) {
    stopVideo(event.target.a.id);
  }
}

function stopVideo(player_id) {
  $.each(players, function(index, val) {
    if (val.getIframe().id != player_id && val.getPlayerState() == 1) {
      val.pauseVideo();
    }
  });
}

Not sure if the code can help someone... it is project specific. I pause the videos from same tab, and I pause all of them when user change tab.

@cferdinandi
Copy link
Owner Author

Looking at this documentation, I think you're right about making this an option.

I'll add an option to disable stopVideo and run your own stuff via a callback if desired. Reopening this ticket.

@cferdinandi cferdinandi reopened this Jul 12, 2016
@cferdinandi
Copy link
Owner Author

Added with v10.1.0 #61

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants