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

Keep youtube iframe video playing when app is in background #562

Open
Topiya opened this issue Sep 16, 2021 · 10 comments
Open

Keep youtube iframe video playing when app is in background #562

Topiya opened this issue Sep 16, 2021 · 10 comments

Comments

@Topiya
Copy link

Topiya commented Sep 16, 2021

In Ionic 5, Is there a way to keep playing youtube iframe video when we put app in background. Because when app goes to background the iframe video gets paused.

I am enabling background mode using this plugin, but it is not working for iframe video.

Can anyone please provide any solution to this?

@Topiya Topiya changed the title ionic 5: Keep youtube iframe video playing when app is in background Keep youtube iframe video playing when app is in background Sep 16, 2021
@keev-studio
Copy link

keev-studio commented Sep 18, 2021

dealing with the same issue now. Audio playing but video not, seems to be no solution for this atm

@the7erm
Copy link

the7erm commented Oct 11, 2021

I've had to deal with this bug for years. The best solution I have is:

document.addEventListener("pause", () => {
    cordova.plugins.backgroundMode.disableWebViewOptimizations();
}, false);
document.addEventListener("resume", () => {
    cordova.plugins.backgroundMode.disableWebViewOptimizations();
}, false);
cordova.plugins.backgroundMode.on('activate', () => {
    cordova.plugins.backgroundMode.disableWebViewOptimizations();
});
cordova.plugins.backgroundMode.on('deactivate', () => {
    cordova.plugins.backgroundMode.disableWebViewOptimizations();
});

I don't like this solution though because the video html 5 element will fire an onpause no matter what and causes the audio to pause and play again.

It'd be nice if you could set it to alwaysDisableWebViewOptimizations(true) so it wouldn't even pause the video elements. I guess it's time to see if I can muddle my way through java and fix the issue. I'm not a very good java developer.

@Topiya
Copy link
Author

Topiya commented Oct 11, 2021

@the7erm I have tried with above solution but it not working for youtube iframe.

@keev-studio
Copy link

keev-studio commented Oct 12, 2021

for me also not working for html5 video tag using mp4, tested on Android 8

@the7erm
Copy link

the7erm commented Oct 16, 2021

@the7erm I have tried with above solution but it not working for youtube iframe.

That's unfortunate. I guess when elements are in an iframe you can't because of browser security stuff. It might also be that youtube has code that forces it to pause.

@mh4ck
Copy link

mh4ck commented Apr 9, 2023

I have the same problem, have you figured it out?

@Topiya
Copy link
Author

Topiya commented Apr 12, 2023

@mh4ck No, I did not get any solution yet.

@arun-commits
Copy link

I managed to fix it by putting a setinterval and play youtube player api every 1s it will work fine :)

@keev-studio
Copy link

I managed to fix it by putting a setinterval and play youtube player api every 1s it will work fine :)

Hi, can you please elaborate a little? Will be very helpful to others if you can provide code example, you env and/or some comments.
Thank you

@philnwoha
Copy link

philnwoha commented Nov 8, 2023

For anyone still looking for a solution, I added an event listener for the visibility changed event and when the browser is hidden, I wait a few seconds then I force the video to play again. See below:
document.addEventListener("visibilitychange", () => { if (document.hidden) { setTimeout(function () { if (player) { player.playVideo(); } }, 3000); } else { } });

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

No branches or pull requests

6 participants