如何在页面加载时随机播放视频? [英] How do I make a random video play on pageload?

查看:48
本文介绍了如何在页面加载时随机播放视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 html/js 中制作一个随机视频播放器.它应该在页面加载时播放不同的视频,一旦一个视频结束,它应该播放另一个视频.

I'm trying to make a random video player in html/js. It is supposed to play a different video on pageload and as soon as one video is over, it should play another one.

HTML

<video width="320" height="240" autoplay>
    <source src="abcdefg.com/example1.mp4" type="video/mp4">
    <source src="abcdefg.com/example2.mp4" type="video/mp4">
</video>

JS

<script>

var videos = [
    [{type:'mp4', 'src':'abcdefg.com/example1.mp4'}],
    [{type:'mp4', 'src':'abcdefg.com/example2.mp4'}],  
];


$(function() {
    var number = Math.floor(Math.random()*videos.length);
    $(this).find('source').each(function(index){ 
          videoSrc = videos[number][index].src;
          $(this).attr('src', videoSrc);
          $('video').load();
          $('video').play();
        });
});

</script>

但是,我当前的代码在每次重新加载页面时都会播放相同的视频,一旦结束,什么也没有发生.

However, my current code plays the same video every page reload and as soon as it's over, nothing happens.

我需要如何优化我的代码,以便它在每次页面加载时自动播放不同的视频+上一个视频结束时?

How do I need to optimize my code so it automatically plays a different video on every pageload + when the previous video is over?

推荐答案

试试这个,我在 video end 属性中添加了一个事件监听器并调用了 newvideo() 函数,这样每次视频结束时都会随机播放一个新的视频从数组加载.我找不到更多视频网址,您可以测试并告诉我它是否适合您.

Try this,I have added an event listener to video end property and called the newvideo() function ,so that every time the video finishes a new random video is loaded from array.I could'nt find more video urls ,you can test and let me know if it works for you.

$(document).ready(function(){

var videos = [
    [{type:'mp4', 'src':'http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4'}],
    [{type:'mp4', 'src':'http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4'}],
    [{type:'mp4', 'src':'http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4'}]
];

// selecting random item from array,you can make your own
var randomitem = videos[Math.floor(Math.random()*videos.length)];

// This function adds a new video source (dynamic) in the video html tag

function videoadd(element, src, type) {
    var source = document.createElement('source');

    source.src = src;
    source.type = type;
   element.appendChild(source);
}
// this function fires the video for particular video tag

function newvideo(src)
{
var vid = document.getElementById("myVideo");
videoadd(vid,src ,'video/ogg');
vid.autoplay = true;
vid.load();
//vid.play();
}
// function call
newvideo(randomitem[0].src)

// Added an event listener so that everytime the video finishes ,a new video is loaded from array
document.getElementById('myVideo').addEventListener('ended',handler,false);

function handler(e)
{
newvideo(randomitem[0].src)

}
  

})

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<video width="320" height="240" autoplay id="myVideo">
  
</video>

这篇关于如何在页面加载时随机播放视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆