Reputation: 29
I am new with Jquery. I am trying to embed a single video with two buttons: Play and Stop. I've noticed that if I put the code with the videoid variable replaced by a real video id (e.g. CkPv2jP9KeY) directly on the html content, the play and stop buttons work. So far, I have this:
<!DOCTYPE html>
<html lang="es">
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="http://www.youtube.com/player_api"></script>
<script>
var player;
function onYouTubeIframeAPIReady() {player = new YT.Player('player');}
if(window.opera){
addEventListener('load', onYouTubeIframeAPIReady, false);
}
</script>
</head>
<body>
<script>
$(document).ready(function(){
$.getJSON("https://gdata.youtube.com/feeds/api/users/diasporaduo/uploads?v=2&alt=jsonc",function(json){
videoid = json.data.items[0].id;
$("#video").append('<iframe id="player" width="560" height="349" frameborder="0" src="http://www.youtube.com/embed/'+ videoid +'?rel=0&wmode=Opaque&enablejsapi=1"></iframe>');
});
});
</script>
<div id="video"></div>
<a href="javascript: void(0)" onclick="player.playVideo(); return false">play</a><br>
<a href="javascript: void(0)" onclick="player.stopVideo(); return false">stop</a>
</body>
</html>
Upvotes: 2
Views: 5252
Reputation: 272006
You can re-arrange your code like this:
https://www.youtube.com/player_api
onYouTubeIframeAPIReady
which does the following:
onReady
eventNotes:
<script>
with src =
https://gdata...&alt=jsonc&callback=functionThatCallsCueVideo
player = new YT.Player({videoId: 'u1zgFlCw8Aw'})
<!-- this could could inside head -->
<script type="text/javascript" src="https://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="https://www.youtube.com/player_api"></script>
<!-- this goes in body -->
<div id="video"></div>
<a href="javascript: void(0)" onclick="player.playVideo(); return false">play</a><br>
<a href="javascript: void(0)" onclick="player.stopVideo(); return false">stop</a>
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('video', {
width: '560',
height: '349',
events: {
'onReady': function () {
$.getJSON("https://gdata.youtube.com/feeds/api/users/diasporaduo/uploads?v=2&alt=jsonc", function (json) {
videoid = json.data.items[0].id;
player.cueVideoById(videoid);
});
}
}
});
}
Upvotes: 1
Reputation: 29
Thanks to Salman A. This is the Jquery working version of his response:
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8" />
<title>Title</title>
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link href="css5/reset.css" rel="stylesheet" />
<link href="css5/main.css" rel="stylesheet" />
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="http://www.youtube.com/player_api"></script>
</head>
<body>
<script>
var player;
$(document).ready(function(){
window.onYouTubeIframeAPIReady = function() {
player = new YT.Player('video', {
events: {
'onReady': function () {
$.getJSON("http://gdata.youtube.com/feeds/api/users/diasporaduo/uploads?v=2&alt=jsonc", function (json) {
videoid = json.data.items[0].id;
player.cueVideoById(videoid);
});
}
}
});
}
});
</script>
<div id="video"></div>
<a href="javascript: void(0)" onclick="player.playVideo(); return false">play</a><br>
<a href="javascript: void(0)" onclick="player.stopVideo(); return false">stop</a>
</body>
</html>
Upvotes: 0