在元素的背景上随机播放youtube视频

时间:2014-10-11 12:26:08

标签: javascript jquery css internet-explorer html5-video

目前,我正在使用此jquery获取youtube视频并在元素的背景中播放它们。

我获得了此代码,可播放单个YouTube视频。但我想制作一个视频列表并随机播放...我现在对Javascript不太满意,所以我真的很感激这方面的一些帮助......

现在我有了这个......

我想制作一个视频列表并随机播放,就像一个改组的播放列表..

$(function() 
{ 
    var videos = ['xJvt2SDV7ck', 'x6DD1k4BAUg', 'xJvt2SDV7ck'];
    var index = Math.floor(Math.random() * videos.length);

    var Video_back = new video_background($("#bgvideo"), 
    {
        "position": "absolute", //Stick within the div
        "z-index": "-1",        //Behind everything
        "loop": true,           //Loop when it reaches the end
        "autoplay": true,       //Autoplay at start
        "muted": true,          //Muted at start
        "youtube": "videos[index]",   //Youtube video id
        "start": 0,                 //Start with 6 seconds offset (to pass the introduction in this case for example)
        "video_ratio": 1.333,      // width/height -> If none provided sizing of the video is set to adjust
        "fallback_image": "videos/main.jpg",    //Fallback image path
    });
});

目前它只播放从列表中随机选择的1个视频(单循环)。 我希望这样做,以便在第一个视频播放完毕后它将转移到另一个视频..

帮助将获得很多赞赏!谢谢您的时间!

1 个答案:

答案 0 :(得分:1)

以下答案基于以下事实:无法确定视频何时播出。长度以毫秒为单位:

$(function() 
{ 
    var videos = 
    [ 
        { id : 'xJvt2SDV7ck', length : 60000 },
        { id : 'x6DD1k4BAUg', length : 125000 },
        { id : 'xJvt2SDV7ck', length : 166000 }
    ];

    function playNext()
    {
        var index = Math.floor(Math.random() * videos.length);

        alert( "Playing next movie => " + videos[index].id );

        var Video_back = new video_background($("#bgvideo"), 
        {
            "position": "absolute", //Stick within the div
            "z-index": "-1",        //Behind everything
            "loop": true,           //Loop when it reaches the end
            "autoplay": true,       //Autoplay at start
            "muted": true,          //Muted at start
            "youtube": videos[index].id,   //Youtube video id
            "start": 0,                 //Start with 6 seconds offset (to pass the introduction in this case for example)
            "video_ratio": 1.333,      // width/height -> If none provided sizing of the video is set to adjust
            "fallback_image": "videos/main.jpg",    //Fallback image path
        });

        setTimeout(function() 
        {
            playNext();
        }, videos[index].length);
    }

    playNext();
});