I'm playing around with YouTubes Javascript API. I've set up a test page on my local enviornment, but the play function is not working. The video loads, and I can press play on the yt video screen, but my link "play" isn't working. Can someone tell me what I'm doing wrong??
I've been following this: .html
<html>
<head>
<script type="text/javascript" src="public/javascripts/swfobject.js"></script>
</head>
<body>
<div id="ytapiplayer">
You will need Flash 8 or better to view this content.
</div>
<script type="text/javascript">
var params = { allowScriptAccess: "always" };
var atts = { id: "myytplayer" };
swfobject.embedSWF(";enablejsapi=1&playerapiid=ytplayer", "ytapiplayer", "425", "365", "8", null, null, params, atts);
function onYouTubePlayerReady(playerId) {
ytplayer = document.getElementById("myytplayer");
}
function play() {
if (ytplayer) {
ytplayer.playVideo();
}
}
</script>
<a href="javascript:void(0);" onclick="play();">Play</a>
</body>
</html>
I'm playing around with YouTubes Javascript API. I've set up a test page on my local enviornment, but the play function is not working. The video loads, and I can press play on the yt video screen, but my link "play" isn't working. Can someone tell me what I'm doing wrong??
I've been following this: http://code.google./apis/youtube/js_api_reference.html
<html>
<head>
<script type="text/javascript" src="public/javascripts/swfobject.js"></script>
</head>
<body>
<div id="ytapiplayer">
You will need Flash 8 or better to view this content.
</div>
<script type="text/javascript">
var params = { allowScriptAccess: "always" };
var atts = { id: "myytplayer" };
swfobject.embedSWF("http://www.youtube./v/OQSNhk5ICTI&enablejsapi=1&playerapiid=ytplayer", "ytapiplayer", "425", "365", "8", null, null, params, atts);
function onYouTubePlayerReady(playerId) {
ytplayer = document.getElementById("myytplayer");
}
function play() {
if (ytplayer) {
ytplayer.playVideo();
}
}
</script>
<a href="javascript:void(0);" onclick="play();">Play</a>
</body>
</html>
Share
Improve this question
asked Jan 8, 2011 at 8:22
thedeepfieldthedeepfield
6,20625 gold badges74 silver badges108 bronze badges
1
-
Your exact code worked just fine for me on IE, Chrome and Firefox. If you're using one of those add such debugging:
function play() { alert(typeof ytplayer); if (ytplayer) { alert(ytplayer.playVideo); ytplayer.playVideo(); } }
what alerts do you see? – user447356 Commented Jan 8, 2011 at 8:44
2 Answers
Reset to default 3That won't work on a local env. You can download e.g. web developer express which has included a tiny web server, and you can run it in localhost.
Your div id is ytapiplayer
not myytplayer
So, change the function onYouTubePlayerReady as
function onYouTubePlayerReady(playerId) {
//ytplayer = document.getElementById("myytplayer");
ytplayer = document.getElementById("ytapiplayer");
}
View the fiddle
to see the code working