javascript - Youtube video not loading with playlists -


i'm having problems enqueuing playlist using youtube iframe api. (https://developers.google.com/youtube/iframe_api_reference#queueing_functions)

i'm using flash show youtube videos. using html5 shows issue videoplayback call called multiple times before video loads.

on loading playlist, video doesn't load. shows in network tab call videoplayback 'pending' until times out after 7.5 minutes @ point tries again , works. playlist, incidentally, has loaded - mousing on youtube iframe shows loaded playlist.

the code replicate below, , issue found following these steps: 1. click channel 2. if channel loads, goto 1, else check network tab.

i know method of replication contrived, i'm seeing 'sometimes' on first load.

the playing channel isn't @ fault - has been seen many different channels.

is code? there work around? there fix?

tested on windows 7 32bit using chrome 28, firefox 22.0 , ie 10

<!doctype html public "-//w3c//dtd xhtml 1.0 strict//en" "w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd"> <html>     <head>         <meta content="text/html;charset=utf-8" http-equiv="content-type">         <title>             youtube test         </title>         <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>         <script type='text/javascript'> var collection = []; var player = null; var playlistenqueued = false;  function clear() {     // removes iframe.     if (player !== null) {         player.destroy();     }      collection = [];     playlistenqueued = false;     player = null; }  function createyt(videoid) {     // clear that's     clear();      // yes, $.ajax, way reduces dependency on jquery     // further purposes of test.     var xmlhttp = new xmlhttprequest();     xmlhttp.onreadystatechange = function() {         if (xmlhttp.readystate == 4 && xmlhttp.status == 200) {             parsejsonresponse(json.parse(xmlhttp.responsetext));         }     }     xmlhttp.open("get", "https://gdata.youtube.com/feeds/users/" + videoid + "/uploads?alt=json", true);     xmlhttp.send(); }  function parsejsonresponse(data) {     var feed = data.feed;     $.each(feed.entry, function(index, entry, list) {         collection.push(entry.id.$t.match('[^/]*$')[0]);     });     playvideo(); }  function playvideo(videoid) {     try {         if (videoid === undefined || videoid === null) {             videoid = collection[0];         }         if (typeof(yt) == 'undefined' || typeof(yt.player) == 'undefined') {             window.onyoutubeiframeapiready = function() {                 playvideo(videoid);             };             $.getscript('//www.youtube.com/iframe_api');         } else {             if (playlistenqueued === true) {                 player.playvideoat(0);             } else {                 player = new yt.player('video', {                     events: {                         'onready':function(event) {                             try {                                 player.cueplaylist(collection);                             } catch (e) {                                 console.error(e);                             }                         },                         'onerror':function(error) {                             console.error(error);                         }                     },                     videoid: videoid,                     width: 425,                     height: 356,                     playervars: {                         autoplay: 1,                     }                 });                  // attaching event listener after object creation due                 // http://stackoverflow.com/questions/17078094/youtube-iframe-player-api-onstatechange-not-firing                 player.addeventlistener('onstatechange', function(state) {                     try {                         statechanged(state);                     } catch (e) {                         console.error(e);                     }                 });             }         }     } catch (e) {         console.error(e);     } }  function statechanged(state) {     // state called on enqueueing playlist     if (state.data == 5) {         playlistenqueued = true;          playvideo();     } }  $(document).on('ready', function() {     var player = $(document).find("#player");      $("a").on('click', function() {         var channel = $(this).attr('data-channel');         createyt(channel);         return false;     }); });          </script>     </head>     <body>         <div class='test'>             <div id='video'>             </div>             <a href='#' data-channel='mit'>mit</a>             <a href='#' data-channel='tedtalksdirector'>ted</a>             <a href='#' data-channel='minutephysics'>minute physics</a>         </div>     </body> </html> 

when using flash, , on waiting video connection timeout , try again following seen in network tab. can see it's 'pending', failed, , tried again after 7.5 minutes.

chrome network tab once video starts playing: chrome network tab on video playback

more images when past 10 reputation...

i've got , running on jsfiddle (http://jsfiddle.net/3bm2v/5/) , seems working ok. did have change

$(document).on('ready', function () {

to

$(function() {

to work in jsfiddle, try link above , see if works now?


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -