Youtube Iframe api player.getPlayerState and player.getCurrentTime is not a function

1k views Asked by At

I am following the youtube iframe api reference Here is how I initialize my player(in onYouTubeIframeAPIReady)

        player = new YT.Player('player', {
            playerVars: {
                modestbranding: 1,
                controls: controls,
                frameborder: 0,
                autoplay: 1,
                autohide: 1,
                showinfo: 0,                %{-- with showinfo set to 0, the modestbranding setting doesn't work --}%
                rel: 0,
                start: startOffset,
            },
            videoId: someVideoId,
            events: {
                'onReady': onPlayerReady,
                'onStateChange': onPlayerStateChange
            },

I am using a div to render the player(not an embedded iframe):

<div id="video-container" style="display:none;">
    <div id="player"></div>
</div>

My problem is that whenever I try to use functions player.getPlayerState and player.getCurrentTime I get error that these functions don't exist on player object. On some further research, I found that we need to have the enablejsapi parameter set to 1 in the video embed url (from here). But since I am not using an iframe, this is ruled out. (I can not use iframe because my video url is dynamic).

The documentation suggest that playerVars while initializing the player has enablejsapi parameter but this too does not work and I still get this error.

The generated iframe using playerVars while initialization does not contain the enablejsapi parameter in src attribute.

So how do I fix this?

0

There are 0 answers