首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在元素的背景上随机播放youtube视频

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

Stack Overflow用户
提问于 2014-10-11 12:26:08
回答 1查看 1.3K关注 0票数 0

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

我得到了这个代码,它将播放youtube上的单个视频。但我想做一个视频列表然后随机播放..。我现在对Javascript不太在行,所以我很想在这方面提供一些帮助.

现在我有这个..。

我想制作一张视频列表,像一张杂乱的播放列表一样随机播放。

代码语言: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段视频(单圈)。我想制作它,这样当第一个视频结束时,它就会转到另一个视频。

我们会非常感谢你的帮助!谢谢您抽时间见我!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-11 22:41:00

以下的答案是基于这样的事实,即没有办法确定什么时候视频已经完成。长度以毫秒为单位:

代码语言:javascript
复制
$(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();
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26314661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档