-2

I know there are several questions for this topic. I even got a great answer here.

The problem is the following. I have a piece of code I can't change that shows the youtube video. I have to adapt to it.

The problem is that the function onPlayerStateChange() never start. Any idea why this happens?

<script src="http://www.youtube.com/player_api"></script>
<script type="text/javascript">
var ytPlayer;
var idplayer;
function onYouTubePlayerReady(playerId) {
    ytPlayer = document.getElementById("ytPlayer");
}

function pauseVideo() {
  if (ytPlayer) {
    ytPlayer.stopVideo();
  }  
}

// when video ends
function onPlayerStateChange(event) {        
    if(event.data === 0) { 
    idplayer=document.getElementById("a_TMBLRWDLH").href;
        window.location.assign(idplayer);
    }
}

j$(document).ready(function(){


    j$('#' + Bnr_vars[0].banner).click(function(){

        if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)))
{
    var info='<iframe width=\"600\" height=\"338\" src=\"http://www.youtube.com/embed/0Bmhjf0rKe8?rel=0&wmode=transparent&modestbranding=1&autoplay=1&enablejsapi=1\" frameborder=\"0\" allowfullscreen></iframe>';
    j$('#player').html(info);
}

});
</script>
Community
  • 1
  • 1
Diego
  • 916
  • 1
  • 13
  • 36
  • 1
    Pro tip: simplify your code and make sure it works, *then* do your fancy stuff. It's easier to debug that way, and you won't have to retrace your steps as much. It clearly works in the example from the linked answer, so I suggest you start from there. Check out this [link](https://developers.google.com/youtube/iframe_api_reference#Getting_Started) – ShadowScripter Aug 21 '13 at 15:39
  • Yeah. Thanks for that. Documentation says I need to have &enablejsapi=1 and this way I can call the events functions. But it's not working. – Diego Aug 21 '13 at 16:41

1 Answers1

0

I have the solution just in case helps someone. I only have to create the new YT.Player when I want the dialog to appear.

j$(document).ready(function(){
    var Bnr_vars = [
        {
            "banner" : "teamworkRowersDLVidOpener",
            "closer" : "teamworkRowersDLVidCloser",
            "dialog" : "teamworkRowersDLVidDialog",
            "auto"   : "false"              // auto open TRUE or FALSE?
        }
    ];

    lightboxCreate(Bnr_vars); // create lightboxes based on array above 

    j$('#' + Bnr_vars[0].banner).click(function(){

        if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)))
        {
            var info=new YT.Player('player', {
                height: '338',
                width: '600',
                videoId: '_Zmr0cUeNBY',
                playerVars : {
                    playerapiid:'player1',
                    rel: '0',
                    wmode: 'transparent',
                    modestbranding: '1'
                },
                events: {
                    'onReady': onPlayerReady,
                    'onStateChange': onPlayerStateChange
                }
            });
            j$('#player').html(info);
        }

    });
Diego
  • 916
  • 1
  • 13
  • 36