Raúl
Raúl

Reputation: 21

seekTo, playVideo, and pauseVideo functions not working in YouTube Player API

I'm working with the YouTube Player API and using the seekTo() function, but it doesn't work. In fact, playVideo() or pauseVideo() doesn't work too. Here is the code:

<script type="text/javascript" src="swfobject.js"></script>
<body>
<div id="divvideo">
<p>You will need Flash 8 or better to view this content.</p>
</div>
<script type="text/javascript">
    var params = { allowScriptAccess: "always" };
    var atts = { id: "video" };
    swfobject.embedSWF(
        "http://www.youtube.com/v/o3nmOw9vKw4?enablejsapi=1&playerapiid=video", "divvideo", "720", "405", "8", null, null, params, atts);

ytplayer = document.getElementById("video");

function play() {
    if (ytplayer) {
        ytplayer.playVideo();
    }

}
</script>
<a href="javascript:void(0);" onclick="play()">Play</a>
</body>

The video appears, but the Play hyperlink doesn't work. What should I do?

Upvotes: 2

Views: 1100

Answers (1)

Joe White
Joe White

Reputation: 97718

In your JavaScript, you have this:

ytplayer = document.getElementById("video");

But when you declare your element, you declare it like this:

<div id="divvideo">

See the mismatch? You're looking for an HTML element whose id is "video", but there isn't one -- your HTML element's id is "divvideo" instead.

You need to either change your getElementById call to take "divvideo", or else change your div to have id="video".

Upvotes: 2

Related Questions