detect when youtube video ends - javascript - not working -
detect when youtube video ends - javascript - not working -
i have youtube videos in db (only urls of them). in form:
http://www.youtube.com/embed/ct1909pdgbu
now, want observe when videos ends , show else in place of video container.
i tried these steps:
1. changed above youtube url to:
http://www.youtube.com/embed/ct1909pdgbu?version=3&enablejsapi=1
2. added js code in videopage.html var player; function onyoutubeplayerapiready() { alert('api ready!'); player = new yt.player('player', { events: { 'onstatechange': onplayerstatechange } }); } // when video ends function onplayerstatechange(event) { if(event.data === 0) { alert('done'); } }
but getting neither "api ready!"
nor "done"
alerts.. doing wrong?
it easy:
var tag = document.createelement('script'); tag.src = "https://www.youtube.com/iframe_api"; var firstscripttag = document.getelementsbytagname('script')[0]; firstscripttag.parentnode.insertbefore(tag, firstscripttag); var player; function onyoutubeiframeapiready() { player = new yt.player('myyoutubevideo_iframe_div', { height: '410', width: '100%', videoid: '{{ video.yt_id }}', events: { 'onready': onplayerready, 'onstatechange': onplayerstatechange } }); } function onplayerready(event) { event.target.playvideo(); } var done = false; function onplayerstatechange(event) { if (event.data == yt.playerstate.ended) { $('#myyoutubevideo_iframe_div').hide(); $('#aftervideo_div').show(); } }
javascript video youtube
Comments
Post a Comment