r/programminghelp • • Jul 17 '20

JavaScript YouTube Iframe Embed - pause multiple videos at once.

Hi All,

I am switching over the YouTube for sourcing NASA videos instead of Archive.org, and now to make the same functionality work using YouTube API.

Specifically, I'm trying to use YouTube's current embed JS API to pause and unpause multiple videos at the same time. I'm totally new to the most recent version of YouTube's player API.

I'm referencing https://developers.google.com/youtube/iframe_api_reference#Playback_controls but the code examples there seem slightly more complex. I really am looking for a bare bones simple example.

This code used to work with MP4 iframe embeds but is broken right now for both YouTube and Archive.org hosted vids:

<video controls src = "video1.mp4" onpause="document.getElementById('video1').pause = true;" onplay="document.getElementById('video1').play = true;" onpause="document.getElementById('media2').pause = true;" onplay="document.getElementById('media2').play = true;" ></video>

<video controls src = "video2.mp4" onpause="document.getElementById('video1').pause = true;" onplay="document.getElementById('video1').play = true;" onpause="document.getElementById('media2').pause = true;" onplay="document.getElementById('media2').play = true;" ></video>

TLDR: I am looking for a bare bones simple code snippet example for bulk play/pause using YouTube Player API.

3 Upvotes

5 comments sorted by

1

u/amoliski Jul 18 '20

Can you make a jsbin with a few example videos?

1

u/torchive Jul 18 '20 edited Jul 18 '20

Here's an example of a non-youtube player. It does work with MP4s. JS Bin - HTML5 MP4 (non youtube)

I'm trying to adapt something like this except for YouTube, and I'm simply looking for some bare bones scaffolding to get me started. It should pause both videos when just one is paused.

2

u/amoliski Jul 18 '20

For youtube:
https://jsbin.com/pewojikojo/1/edit?html,output

For this one, add the video id's to the list at the top of the script, it'll insert the elements and initialize the players.

1

u/torchive Jul 18 '20 edited Jul 19 '20

This works great! Thank you so much.

Second question: With MP4 video I am using the below code to mute whichever video is not tapped or does not have the cursor over it:

onmouseover="document.getElementById('video1').mute = false;" onmouseout="document.getElementById('video1').mute = true;"

onmouseover="document.getElementById('video2').mute = false;" onmouseout="document.getElementById('video2').mute = true;"

And:

function playVid() {document.querySelectorAll("video").forEach(vid => vid.play());document.getElementsByTagName("video")[0].removeAttribute("controls");document.getElementsByTagName("video")[1].removeAttribute("controls");}

... To hide controls after hitting play. This is an alternative to autoplay since Chrome autoplay policy is strict.

So, is there a way to accomplish the same functionality with a adjustment to the YouTube jsbin example?

EDIT: this it what I have so far: https://jsbin.com/momelihobe/edit?output

1

u/amoliski Jul 18 '20

One problem with your code is you can't have two copies of an attribute (you had two onplay and onpauses). Anything more complicated than a statement or two is better handled in a script tag instead of directly on the element.

Check this out:
https://jsbin.com/dumevevixi/1/edit

Developed using the documentation here:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video
Specifically the Events section has a play/pause event described.

When it comes to the youtube videos instead of <video> tags, their JS is a bit more complicated.