PHP?? No. You’re making a classic rookie mistake of thinking PHP can manage anything on a page after it has been output. It can’t because it runs on the server. The page now resides in the browser. Only JavaScript or its many variants like jQuery can run on a browser. (Well, there are extensions for other languages, but you cannot expect users to have them installed)
It’s actually impossible to access the YT play button and disable it with JS because the button lies within an externally sourced iframe. Being able to access it is seen as cross-site scripting, which is blocked for security reasons.
Just having many embedded YT players on a page really drags down page load speeds even if they are not playing. One scheme you could implement is to merely have static images representing embedded players. When any one is clicked, your script puts up a modal that contains the actual player clicked, having been dynamically loaded by the click event on the static image. Any click outside the player kills the modal and the embed it contained. Thus users can only play one vid at a time.
If you ask me, it’s not worth doing anything to protect user’s bandwidth. It’s not like these all auto-play. If someone starts multiple streams, it’s their own fault that they took up all available bandwidth.
Thread Starter
jjon
(@jjon)
Thanks @bcworkz, that was quite helpful. Rookie mistake, yes. I can remember making the same mistake when I first built something in PHP decades ago.
It’s actually impossible to access the YT play button and disable it with JS because the button lies within an externally sourced iframe. Being able to access it is seen as cross-site scripting, which is blocked for security reasons.
In a different context, and without all this PHP, I embeded a vimeo player in a page and found it had an easily accessible API so that I could do this:
<script>
function unloadVimeo(player) {
var p = new Vimeo.Player(document.getElementById(player));
p.unload();
}
</script>
where player is the ID of a dom element. YouTube has no similar API?
I like your static images with modal player idea, but I’m trying to keep the extra code to a minimum
As to user’s bandwidth; yeah, you’re right.
j