微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何防止视频数组播放同一视频两次?

如何解决如何防止视频数组播放同一视频两次?

我试图在不播放视频两次的情况下播放视频 1 到 3。我正在使用 videos.shift(); 从阵列中删除一个视频,但我不确定如何在播放一次后删除其他 2 个视频。

    var videos = ["video1","video2","video3"];
    videos.shift();
    var player1 = document.getElementById("video");
    function setMp4Source1() {
        var currentVideoIndex = Math.floor(Math.random() * videos.length);
        var currentVideo = "videos/" + videos[currentVideoIndex] + ".mp4";
        player1.src = currentVideo;
        player1.load();
        player1.play();
    }
    player1.addEventListener('ended',nextVideo,false);
    function nextVideo() {
        setMp4Source1();
    }

解决方法

如果我理解正确,videos 是需要按顺序播放的视频队列,除此之外别无他物。

我会将所有队列管理规范化到 nextVideo() 函数中,这样你第一次玩就没有什么特别的了。因此:

var videos = ["video1","video2","video3"]

var player1 = document.getElementById("video")
function setMp4Source1(theVideo) {
    var currentVideo = "videos/" + theVideo + ".mp4"
    player1.src = currentVideo
    player1.load()
    player1.play()
}
player1.addEventListener('ended',nextVideo,false)

function nextVideo() {
    let theVideo = videos.unshift()
    setMp4Source1(theVideo)
}
nextVideo() // start the chain here!

现在,这不会像您最初的示例那样播放随机视频 - 只是队列中的下一个。您可以使用 nextVideo() 中的 splice() 播放随机视频并将其从队列中删除:

function nextVideo() {
    let randomIndex = Math.floor(Math.random() * videos.length)
    let theVideo = videos[randomIndex]
    videos.splice(randomIndex,1) // remove 1 element starting at the index
    setMp4Source1(theVideo)
}

当然,接下来您需要添加一个检查以确保您没有访问空数组...

function nextVideo() {
    if( videos.length < 1) {
       // no moar videos! :(
       // probably best to bail out here and avoid further chaining.
       // if you're clever,you'll add a placeholder image to let 
       // the user know there's no more videos.
       // maybe something from http://placekitten.com/
       player1.removeEventListener('ended',false)
       
       sizer_x = player1.clientWidth
       sizer_y = player1.clientHeight
       const kitty = '<img src="http://placekitten.com/' + sizer_x + '/' + sizer_y + '"/>'
       const kittyImg = document.createElement(kitty)
       player1.parentNode.replaceChild(kittyImg,player1)

       return
    }
    let randomIndex = Math.floor(Math.random() * videos.length)
    let theVideo = videos[randomIndex]
    videos.splice(randomIndex,1) // remove 1 element starting at the index
    setMp4Source1(theVideo)
}
,

跟踪使用 Set 对象播放的索引号。 Set 对象类似于数组,但只包含唯一项。详细信息在以下代码段中进行了评论。

const videos = [
  "vs8s3.mp4","vs12s3.mp4","vs21s3.mp4","vs2s3.mp4"
];
const player = document.querySelector("video");
// Create a Set object
const played = new Set();

function playVideo() {
  let current = Math.floor(Math.random() * videos.length);
  const base = 'https://glpjt.s3.amazonaws.com/so/av/';
  const source = base + videos[current];
  
  /* Check if video has already been played */
  // If Set {played} DOES NOT have the {current} index number...
  if (!played.has(current)) {
    // Add {current} index number to Set {played}
    played.add(current);
    player.src = source;
    player.play();
  } 
    /* or if Set {played} does have {current} index number AND Set {played} 
    DOES NOT have the same amount of numbers as Array {videos}... */
    else if (played.size != videos.length) {
    // Run the function again
    playVideo();
  } 
  // Otherwise end function
  else {
    return false;
  }
  /* OPTIONAL: Display the indexes of each video played in order */
  console.clear();
  console.log(JSON.stringify(Array.from(played)));
};

player.addEventListener('ended',playVideo,false);
player.addEventListener('click',false);
video {
  cursor: pointer;
}
<video poster='http://simgbb.com/background/W46xw0TswdDx.jpg' width='360'></video>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。