5

I am developing a chrome extension that plays videos in the background (in an inactive tab) and I remarked that Chrome automatically pauses any video or media before the tab is focused. This is a real problem for me as I need to play videos in unfocused tabs.

I have made some research but I haven't found any solution to disable this behavior.

Is there any way to solve this ? A local solution would be great (from chrome params), even if a javascript one would be better (from the extension).

Pritish Vaidya
  • 21,561
  • 3
  • 58
  • 76
David Seroussi
  • 1,650
  • 2
  • 17
  • 34
  • How is new tab opened without gaining focus? – guest271314 Dec 24 '16 at 17:45
  • @guest271314 Well it's not opened without gaining focus. The extension does its work in a tab that was opened with purpose of being "backgrounded" – David Seroussi Dec 24 '16 at 23:47
  • _"I remarked that Chrome automatically pauses any video or media before the tab is focused."_ , _"it's not opened without gaining focus."_ If tab gains focus, media playback begins, yes? – guest271314 Dec 25 '16 at 00:10
  • Yes, so ? I need videos to plau without focus – David Seroussi Dec 25 '16 at 01:33
  • Could not reproduce video not playing when new tab is opened. Is Question how to achieve opening tab without gaining focus on the newly opened tab? – guest271314 Dec 25 '16 at 01:46
  • Just go on youtube, and open any video in a new tab. You'll see that video doesn't start until you focus the new tab – David Seroussi Dec 25 '16 at 10:10
  • Not sure how youtube is related to Question? youtube is not mentioned at actual text of Question. – guest271314 Dec 25 '16 at 16:10
  • You just told me that you *"could not reproduce video not playing when new tab is opened"*. So to reproduce this, just open a youtube video in a new tab so you can see that the video doesn't play until you focus the tab. I really don't understand how it's that complicated. – David Seroussi Dec 25 '16 at 16:23
  • That is correct. Could not reproduce ` – guest271314 Dec 25 '16 at 16:29
  • It's not a different question because chrome automatically pauses **any** media, which includes youtube videos. You don't need to try to reproduce anything more than opening a media in a new tab (the easier media to try for me is youtube). You'll see that if a video is opened in a new tab without being focused, then the media won't play until the tab is focused. I feel like I have said that 3 times already. – David Seroussi Dec 25 '16 at 16:50
  • _"It's not a different question because chrome automatically pauses any media"_ Have not found that to be accurate. The cases between ` – guest271314 Dec 25 '16 at 16:52
  • Man. I don't know how you can not reproduce this. Just go to youtube, right click on any video and click open in a new tab. Does the video start without you switching to the new created tab ? – David Seroussi Dec 25 '16 at 16:56
  • youtube is different from dynamically creating a `video` element. There is no option for "open in a new tab" when right-clicking a youtube video. Again, "youtube" is not described at actual text of original Question. – guest271314 Dec 25 '16 at 16:59
  • I never said I was creating a video element dynamically. And do not right click a youtube video that is already playing. Right click a preview image on the home page of youtube – David Seroussi Dec 25 '16 at 17:03
  • _"I never said I was creating a video element dynamically."_ You did not mention "youtube" at original Question, either. You are including details at comments which should probably be included at original Question, to avoid confusion. _"I remarked that Chrome automatically pauses any video or media before the tab is focused."_ The issue is not chrome, but rather how youtube implements playback of the media youtube serves. – guest271314 Dec 25 '16 at 17:06
  • Yes I did not mention youtube because it's not youtube in particular. I just find it easier to reproduce the behavior with youtube. You're wrong. It's about chrome, not youtube. I tried the answer below and it worked well. – David Seroussi Dec 25 '16 at 17:10
  • _"Yes I did not mention youtube because it's not youtube in particular"_ Well, cannot reproduce dynamically creating a ` – guest271314 Dec 25 '16 at 17:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/131444/discussion-between-david-seroussi-and-guest271314). – David Seroussi Dec 25 '16 at 17:24
  • I can't repro either on my osX chromes, even with my flags raised up, but does the `pause` event fire ? If so, does calling `play` in this event works ? You may want to do a double check with the [visibilityAPI](https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API) – Kaiido Dec 30 '16 at 05:54

2 Answers2

5

I am not familiar with a javascript solution, but there is a local option.

This behavior can be adjusted with the flag:

Gesture requirement for media playback

When this flag is enabled, videos will not be played in the background. This flag can be disabled on the chrome://flags page or by adding to the launch options:

--disable-gesture-requirement-for-media-playback

Exikle
  • 1,155
  • 2
  • 18
  • 42
jobB
  • 411
  • 3
  • 10
  • Thank you this is working well, I am just waiting if someone has a javascript solution for this, even if I doubt it's possible to do it from an extension. – David Seroussi Dec 23 '16 at 13:42
  • 2
    It is not possible to change flags in javascript / from an extension, see for example http://stackoverflow.com/questions/17060363/google-chrome-how-can-i-programmatically-enable-chrome-flags-some-of-the-mod, http://stackoverflow.com/questions/32210069/can-a-chrome-extension-toggle-switches-normally-set-as-command-line-flags, http://stackoverflow.com/questions/18951644/how-to-enable-google-chrome-chrome-flags-values-using-javascript – user812786 Dec 29 '16 at 14:37
  • 2
    The flag does not exists anymore ? (chrome 60 here) - any other option ? – gordie Aug 09 '17 at 14:58
0

Recently i came across same issue where, on tab focus out video was getting paused. Looks like it's a feature provided by chromium when audio track is not present in the video. To fix the issue simplest solution would be to edit the video and add placeholder audio track if it's not present. I did not faced pause issue with video having audio.

More details: https://developer.chrome.com/blog/media-updates-in-chrome-63-64/#background-video-track-optimizations