Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Youtube iframe picture-in-picture mode

I'd like to use JavaScript to enable PiP on Youtube videos. I am able to do so on html <video>s but it seems impossible to do so on Youtube <iframe>s. Has someone any lead on this?

like image 681
Harry Avatar asked Mar 26 '20 22:03

Harry


People also ask

Can you play YouTube in picture-in-picture mode?

Although the iOS-based devices have only recently gotten this functionality, the YouTube Android app has had it for years. You can see a supported video in a small, minimized window using the PiP functionality.

How do you activate picture-in-picture on YouTube?

To use picture-in-picture in the YouTube app, just select a video to watch and then exit out of YouTube by swiping upwards from the bottom of the screen. The video will transfer to a mini player that can be moved around the display.

Why is picture-in-picture not working for YouTube?

Picture in picture is a YouTube premium feature or only works in some specific countries (like the USA). You may encounter the error at hand if your region in YouTube's settings is set to a country where the picture in picture feature is not supported by YouTube.


2 Answers

Users may be able to right click twice (to bring up the html5 menu) and select Picture in Picture, but it seems to be impossible to trigger it programmatically from outside the iframe due to the same origin policy for iframes. I don't imagine that the &origin= parameter in the iframe URL actually changes the headers sent from youtube, it didn't seem to work in my testing. SecurityError: Blocked a frame with origin from accessing a cross-origin frame

The youtube iframe api gets around this by using postMessage, however there are no messages implemented at the moment to execute video.requestPictureInPicture(). There seems to be a togglePictureInPicture function defined inside the player base.js script but this doesn't seem to be exposed through the message passing API

like image 129
Mahir Ahmed Avatar answered Sep 21 '22 11:09

Mahir Ahmed


I always use the next line in the console, on the youtube page:

document.getElementsByTagName('video');

This will show a HTML Collection, open it and right click on the first position of the collection, store it as global variable, this will generate something like temp1. Then you have to write the next line:

temp1.requestPictureInPicture();

And that's it.

like image 28
Eduardo Martínez Sánchez Avatar answered Sep 23 '22 11:09

Eduardo Martínez Sánchez