【问题标题】:Playing youtube video randomly on element's background在元素的背景上随机播放 youtube 视频
【发布时间】:2014-10-11 12:26:08
【问题描述】:

目前,我正在使用这个 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 个视频(单循环)。 我想让它在第一个视频结束时转到另一个视频..

我们将非常感谢您的帮助!感谢您的宝贵时间!

【问题讨论】:

  • 请定义什么是“不起作用”。你有控制台错误吗?
  • 您可能需要这样做:"youtube": videos[index],。正如你所拥有的,youtube 正在寻找一个 id 为“[index]”的视频
  • 感谢您的反馈!它开始随机播放视频!但我也想播放我上面列出的那些视频作为播放列表,这样它就不会只播放单个视频,而是循环播放多个视频。
  • 好的。那么这个“video_background”是什么?是插件吗?也许有一种方法可以创建播放列表,或者,您需要确定视频何时结束。也许有一个回调说“视频完成”?此时您可以再次调用您的代码。当然,对于这么小的视频数组,可能会再次选择相同的视频。因此,您可能希望防止这种情况发生...
  • 非常感谢您的帮助.. 我今天需要完成这个项目... :( 目前我正在使用我从 Codecanyon 获得的“Easy Video Background”插件。我看了一些文件,似乎没有“视频完成”的回调功能......

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


【解决方案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();
});

【讨论】:

  • 以上内容未经测试,但希望与以上内容类似
  • 非常感谢...我欠你很多时间...我尝试了您在上面提供的代码。它仍在循环播放单个 youtube 视频。我还尝试在循环功能上设置“假”,但它最终没有在一个视频结束时停止视频播放......
  • 顺便说一句,1ne-studio.com/test/index2.html id okite pass whatsup 是我正在研究的。
  • 它确实会尝试播放另一部电影。但是,如前所述,它很可能会再次选择同一部电影。此外,如果您在浏览器中按 F12,然后加载您的页面,您会看到很多控制台错误。有很多(其他)东西被“破坏”,可能会对上述代码产生不利影响......
  • 我在上面的代码中添加了alert 语句。将它放在您的实时站点中,您会看到它正在尝试播放另一部电影。
猜你喜欢
  • 2014-09-24
  • 2017-09-15
  • 1970-01-01
  • 2018-10-12
  • 2017-11-20
  • 1970-01-01
  • 1970-01-01
  • 2013-03-27
  • 1970-01-01
相关资源
最近更新 更多