javascript - YouTube API - onPlayerStateChange -
i'm using youtube api in conjunction cyclone slider. goal pause slideshow once youtube starts playing. i'm using following code works nicely:
<script> var tag = document.createelement('script'); tag.src = "//www.youtube.com/iframe_api"; var firstscripttag = document.getelementsbytagname('script')[0]; firstscripttag.parentnode.insertbefore(tag, firstscripttag); </script> <script> var player; function onyoutubeplayerapiready() { player = new yt.player('video', { events: { 'onstatechange': onplayerstatechange } }); } function onplayerstatechange(event) { if(event.data === 1) { $(".cycle-slideshow").cycle('pause'); } if(event.data === 2) { $(".cycle-slideshow").cycle('resume'); } } </script>
however, seems work if refresh of page. if navigate between pages , return homepage, no longer work.
any suggestions why case? i've tried few suggestions found on google couldn't work. i'm little lost on one.
any appreciated.
try following code, works me -
function loadyoutube(targetid){ ytplayer = new yt.player(targetid, { events: { 'onstatechange': function(event){ /** yourube api -1 (unstarted) 0 (ended) 1 (playing) 2 (paused) 3 (buffering) 5 (video cued) **/ if (event.data == 1) { //do work here } console.log(event.data) } } }); }