2

I have this code in html:

        <div class="flex-video widescreen youtube">
            <iframe id="ytplayer" type="text/html" width="640" height="360" src="https://www.youtube.com/embed/{{Video.v_id}}?autoplay=1&enablejsapi=1&autohide=1" frameborder="0" allowfullscreen> </iframe>
        </div>

and this code in Javascript:

        function onYouTubePlayerReady(playerId) {
            console.log("player ready");
            ytplayer = document.getElementById("ytplayer");
        }

When I type in Firefox console this:

ytplayer.playVideo();

I get the not function error:

TypeError: ytplayer.playVideo is not a function

Also the console.log("player ready"); does not print at all.

Can anybody help me?

I want to controll the player with javascript api not with the IFrame api. And I want the video player to be in html5.

edit: I have the jspapi enabled (enablejsapi=1)

3
  • This is impossible; the YouTube javascript API (which was many years ago before there was any HTML5 video on YouTube) can only control an embedded SWF. The iFrame API was created specifically to let you work with HTML5 video with SWF fallback, so that's the one to use. Commented Oct 31, 2014 at 4:11
  • @jlmcdonald So can I establish manually the <iframe> then control it with the IFrame API without using dhe <div id="player"></div> ? Commented Oct 31, 2014 at 8:34
  • You won't have to have a <div> if you already included the <iframe> in the DOM ... just give your <iframe> an ID attribute and pass that to the YT.Player constructor (when you do so, leave out the width, height, and videoId parameters of the constructor object, as your iframe already establishes those). Commented Nov 1, 2014 at 5:52

2 Answers 2

6

What works for me is adding the youtube by file and from the JS and then using the onYoutubePlayerAPIReady event like this:

 window.onYouTubePlayerAPIReady = function(){
            var player = new YT.Player('player', {
                height: '390',
                width: '640',
                videoId: 'M7lc1UVf-VE',
                events: {
                    'onReady': onPlayerReady,
                    'onStateChange': onPlayerStateChange
                }
            });
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Works for me a year later!
1

If you follow the doc of the YouTube API Player

HTML

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <div id="player"></div>
</body>
</html>

JS

var tag = document.createElement('script');

tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

// 3. This function creates an <iframe> (and YouTube player)
//    after the API code downloads.
var player;

function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
        height: '390',
        width: '640',
        videoId: 'l-gQLqv9f4o',
        events: {
            'onReady': onPlayerReady,
                'onStateChange': onPlayerStateChange
        }
    });
}

// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
    event.target.playVideo();
}

function onPlayerStateChange(event) {
    if (event.data == YT.PlayerState.PLAYING) {

    }
}

function stopVideo() {
    player.stopVideo();
}

Live demo

1 Comment

I really appreciate your response. I managed the iframe api to work developers.google.com/youtube/iframe_api_reference but this does not work very well on my meteorjs application I&#39;m trying to implement the JavaScript player api developers.google.com/youtube/js_api_reference i dont want to do this with swf player but with html5 as primary then fallback in swf. Thank you in advance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.