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

YouTube player will not autoplay if window is not focused #7

Closed
Fauntleroy opened this issue Nov 1, 2015 · 8 comments · Fixed by #13
Closed

YouTube player will not autoplay if window is not focused #7

Fauntleroy opened this issue Nov 1, 2015 · 8 comments · Fixed by #13

Comments

@Fauntleroy
Copy link
Contributor

When you use a page with react-player in it, YouTube videos will not autoplay when the window does not have focus. While I'm certain YouTube has good intentions here, my application requires YouTube videos to play automatically regardless of whether the window is focused or not. Here are a few things I've tried but don't seem to work:

  • Run .playVideo() after the onReady event of the YouTube player fires
  • Constantly run window.focus() in the hope that they have a listener there (nope)
@cookpete
Copy link
Owner

cookpete commented Nov 9, 2015

This seems to happen only when the SDK and iframe have not been loaded yet. That is, playing a youtube video straight after a youtube video works fine in the background because the player is already loaded and ready, but playing a youtube video after a Soundcloud track, or from a fresh load, will not work.

A possible (but annoying) solution would be to constantly have the youtube SDK and frame loaded as long as the ReactPlayer lives, but this wouldn't solve all the issues with this.

@cookpete
Copy link
Owner

cookpete commented Nov 9, 2015

A quick google search returns a few good results with more about this "feature". Notably a post from Francois Beaufort from Google, with various people complaining about this problem.

@Fauntleroy
Copy link
Contributor Author

According to google, if the tab has had focus at any point it should be able to autoplay media. In practice this is not the case. I need to build some test cases out and see if I can find out what the truth is.

@Fauntleroy
Copy link
Contributor Author

It looks like the issue is definitely related to players that use iframes.

My theory is that since the content served by the YouTube player's iframe is from a different domain, the tab focus flag is reset every time that player is destroyed and recreated. I'm going to figure out a way to keep the YouTube player's iframe around between player changes, which should eliminate this issue.

@cookpete
Copy link
Owner

This would eliminate the issue, but we would still need an initial youtube play/click to "activate" the iframe, and I guess there would still be issues with playlists that don't begin with a youtube video. Either way, it sounds like the fix will not be very pleasant..

@Fauntleroy
Copy link
Contributor Author

Yup, you'll have to "prime" the Vimeo and YouTube players, but that shouldn't be too difficult. I'm gonna try my best not to make your codebase disgusting in the process :)

@Fauntleroy
Copy link
Contributor Author

After multiple attempts at trying to "preserve" the iframe by other means, it looks like the only way we can circumvent this issue is by making sure the iframe is never moved or destroyed at all. Moving or destroying the iframe causes its contents to load again, revoking its autoplay permission. Since this component currently relies heavily on mounting and unmounting components to work, it'll take a serious rewrite to fix this.

@cookpete
Copy link
Owner

Hopefully fixed in 0.3.0

@cookpete cookpete mentioned this issue Mar 2, 2016
david-hub024 pushed a commit to david-hub024/React_VideoPlayer that referenced this issue Dec 23, 2018
david-hub024 pushed a commit to david-hub024/React_VideoPlayer that referenced this issue May 23, 2020
albanqoku added a commit to albanqoku/react-player that referenced this issue Feb 24, 2021
Webmaster1116 added a commit to Webmaster1116/video-player that referenced this issue May 20, 2021
webmiraclepro added a commit to webmiraclepro/video-player that referenced this issue Sep 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants