Youtube视频未加载播放列表

时间:2013-07-18 15:00:04

标签: javascript youtube-api connection-timeout

我在使用YouTube IFrame API排队播放列表时遇到问题。 (https://developers.google.com/youtube/iframe_api_reference#Queueing_Functions

我正在使用flash来显示youtube视频。使用HTML5显示了另一个问题,即视频加载前多次调用videoplayback调用。

加载播放列表时,视频本身无法加载。它显示在网络选项卡中 作为对“正在等待”的视频播放的调用,直到它在7.5分钟后超时,此时它再次尝试并且一切正常。顺便提一下,播放列表已成功加载 - 鼠标悬停在youtube上,iframe显示已加载的播放列表。

下面是复制此代码的代码,按照以下步骤找到问题: 1.单击一个频道 2.如果频道加载,请转到1,否则请检查网络选项卡。

我知道复制的方法是人为的,但我有时会看到这种情况 在第一次加载时。

播放频道没有错 - 很多不同的频道都有这种情况。

这是我的代码吗?有工作吗?有修复吗?

使用Chrome 28,Firefox 22.0和IE 10在Windows 7 32位上进行测试

<!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 the iframe.
    if (player !== null) {
        player.destroy();
    }

    collection = [];
    playlistEnqueued = false;
    player = null;
}

function createYT(videoId) {
    // Clear anything that's up currently
    clear();

    // Yes, this could be $.ajax, however this way reduces the dependency on jQuery
    // further for the purposes of this 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 to
                // 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) {
    // This state will be called on enqueueing a 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>

使用闪光灯时,等待视频连接超时并再次尝试时,可在网络选项卡中看到以下内容。你可以看到它正在“待定”,失败,然后在7.5分钟后再次尝试。

视频开始播放后,

Chrome网络标签: Chrome network tab on video playback

当我获得10点声望时的更多图片......

1 个答案:

答案 0 :(得分:0)

我已经在jsFiddle(http://jsfiddle.net/3Bm2V/5/)上运行并且它似乎工作正常。我确实需要改变

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

$(function() {

要让它在jsFiddle中运行,请尝试上面的链接,看看它现在是否适合你?