Eduardo Plaza
Eduardo Plaza

Reputation: 29

YouTube IFrame API play/stop not working

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

Answers (2)

Salman Arshad
Salman Arshad

Reputation: 272006

You can re-arrange your code like this:

  1. Include https://www.youtube.com/player_api
  2. Create an empty div element, preferably same size as the video
  3. Implement onYouTubeIframeAPIReady which does the following:
    • Create a blank YouTube iframe player
    • Load the video in onReady event

Notes:

  • You do not need jQuery for retrieving JSON data from GData. You can simply create a <script> with src = https://gdata...&alt=jsonc&callback=functionThatCallsCueVideo
  • It is possible to re-arrange the code. E.g. You could load the JSON first, then call player = new YT.Player({videoId: 'u1zgFlCw8Aw'})

HTML:

<!-- 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>

JavaScript:

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);
                });
            }
        }
    });
}

Demo

Upvotes: 1

Eduardo Plaza
Eduardo Plaza

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

Related Questions