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

What's about events? #27

Closed
MB116 opened this issue Dec 8, 2019 · 11 comments
Closed

What's about events? #27

MB116 opened this issue Dec 8, 2019 · 11 comments

Comments

@MB116
Copy link

MB116 commented Dec 8, 2019

Need some events when video start/stop, to control playing another video blocks

@Garbee
Copy link
Contributor

Garbee commented Dec 11, 2019

Wouldn't you link to the events on the main YouTube video once embedded?

I think we should expose an event when that occurs. Although a MutationObserver could possibly fill the role just fine without us needing to do any extra work. Worth looking into and explaining though.

@paulirish
Copy link
Owner

paulirish commented Dec 18, 2019

I think this is roughly supported via the jsapi which will be possible to use via #20 (comment)

It'll require some experimentation though.

@lborgman
Copy link

I need this so I am implementing it in a fork now. Some thoughts:

  1. "enabjejsapi=1" is needed. How to implement that is trivial. I will use "videoparams=...".
  2. To be of any use the "onYouTubeIframeAPIReady" must be implemented since it sets up the event handlers.
  3. "onYouTubeIframeAPIReady" needs the iframe DOM element.
  4. Since the iframe element is not created until the video is played the "onYouTubeIframeAPIready" must be setup after that.
  5. I am adding a user defined function that will be called to do this - inside "addIframe".

Any thoughts on this?

@lborgman
Copy link

So I implemented this. But there is a major obstacle: onYouTubeIframAPIReady
It accepts currently only a single function. That can't work in cases like this. It must accept an array (or something similar).

Is there someone who can reach the developers for that API and tell them this?

@Garbee
Copy link
Contributor

Garbee commented Mar 12, 2020

Why won't a single function work? What is the use-case where an array is required to get the expected functionality?

@lborgman
Copy link

My use-case is a web component.

@lborgman
Copy link

Eh, sorry, my bad. It works fine with a single function.

@Garbee
Copy link
Contributor

Garbee commented Mar 13, 2020

My use-case is a web component.

That's not a use-case, that's just what you're trying to use it in. A use case defines what your goal is, how you're approaching the problem, and what about the external API isn't allowing you to move forward.

Eh, sorry, my bad. It works fine with a single function.

So there is no need to try and get an array to be supported by the YouTube API?

@lborgman
Copy link

So there is no need to try and get an array to be supported by the YouTube API?

No, it was my mistake. (As I guess anyone here realize you just wrap the old function in onYouTubeIframAPIReady in your new function.)

@lborgman
Copy link

Something like this for wrapping:

`

(() => {
    function myOnLoaded() {
        console.warn("myOnloaded dispatch gotapi")
        window.dispatchEvent(new Event("my-gotapi"));
    }
    async function startLoading() {
        const tag = document.createElement('script');
        tag.src = 'https://www.youtube.com/iframe_api';
        await new Promise(function (resolve) {
            if (document.readyState !== "loading") return resolve();
            document.addEventListener("DOMContentLoaded", resolve);
        });
        document.body.append(tag);
    }
    if (window["YT"]) {
        myOnLoaded();
        return;
    }
    if (!window["onYouTubeIframeAPIReady"]) {
        window["onYouTubeIframeAPIReady"] = myOnLoaded;
    } else {
        const theirOnLoaded = window["onYouTubeIframeAPIReady"];
        window["onYouTubeIframeAPIReady"] = () => {
            theirOnLoaded();
            myOnLoaded();
        }
    }
    startLoading();
})();

`

@paulirish
Copy link
Owner

as of 0.1.0, you can now easily turn on the js api: https://github.com/paulirish/lite-youtube-embed#custom-player-parameters

with enablejsapi=1 ... you can then use https://developers.google.com/youtube/iframe_api_reference

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

4 participants