javascript - YouTube API to play random video from tag -
i have youtube api got link...
https://developers.google.com/youtube/iframe_api_reference
it's working okay, looking way play random video based on tag...is possible? there tutorial on it?
here code
<!-- 1. <iframe> (and video player) replace <div> tag. --> <div id="player"></div> <script> // 2. code loads iframe player api code asynchronously. var tag = document.createelement('script'); tag.src = "https://www.youtube.com/iframe_api"; var firstscripttag = document.getelementsbytagname('script')[0]; firstscripttag.parentnode.insertbefore(tag, firstscripttag); // 3. function creates <iframe> (and youtube player) // after api code downloads. var player; function onyoutubeiframeapiready() { player = new yt.player('player', { height: '390', width: '640', videoid: 'u1zgflcw8aw', events: { 'onready': onplayerready, 'onstatechange': onplayerstatechange } }); } // 4. api call function when video player ready. function onplayerready(event) { event.target.playvideo(); } // 5. api calls function when player's state changes. // function indicates when playing video (state=1), // player should play 6 seconds , stop. var done = false; function onplayerstatechange(event) { if (event.data == yt.playerstate.playing && !done) { settimeout(stopvideo, 6000); done = true; } } function stopvideo() { player.stopvideo(); } </script>
make ajax function call handle request
function request_yt_videoid(vid) { // code player.loadvideobyid(vid); }
visit google developer site: https://developers.google.com/youtube/iframe_api_reference#loadvideobyid
Comments
Post a Comment