循环动画HTML5 – 仅在当前视频播放完毕后替换视频

huangapple go评论64阅读模式
英文:

Looping animations HTM5 - replacing video only after current video has finished playing

问题

以下是代码部分的翻译:

[![显示图像描述][1]][1]

我有以下代码,它显示一个循环中的球体动画。我想要能够控制动画的变化,以便如果我点击播种/深度搜索按钮,它只会在当前动画完成后更改视频。需要包括视频预加载和事件冒泡处理,以避免在一个动画仍在播放时尝试切换不同的动画。也许采用最新点击的按钮动画 - 跳过尝试播放早期点击的动画。

[代码链接](https://codesandbox.io/s/fragrant-thunder-czctj7)

<!DOCTYPE html>
<html>
<body>

<script>

  let animationStackObject = [{
      "state": "idol",
      "path": "./orb2/idol.mp4"
    },
    {
      "state": "seeding",
      "path": "./orb2/seeding.mp4"
    },
    {
      "state": "deepsearch",
      "path": "./orb2/deepsearch.mp4"
    }
  ]

  let videoController = {
    vid: document.getElementById("myVideo"),
    init: function(){
      //let vid = document.getElementById("myVideo");
      //vid.src = getAnimation("idol", animationStackObject)

      //setCurrentState("idol");
      //this.playVid()
      //playVideo()//loopable
    },
    getAnimation: function(state, animationStackObject){
     //-- 返回所请求状态的正确动画堆栈元素
    },
    playVid: function() {
      let vid = document.getElementById("myVideo");
      vid.play();
      console.log(vid.duration);
      console.log(vid.currentTime);
      //1个循环中的视频持续时间为5秒 - 启动计时器
    },
    pauseVid: function() {
      let vid = document.getElementById("myVideo");
      vid.pause();
    },
    videoEnded: function() {
      console.log("结束。");
      this.playVid()
    },
    changeMood: function(state){
      //视频仍在播放吗?

      //在视频完成时断开循环并发送新视频以播放
          
      //setCurrentState("idol");
      //将当前视频更改为新视频路径
      //播放循环
      console.log(state);
      this.changeVideo();
    },
    changeVideo: function() {

      var vid = document.getElementById('myVideo');
      var source = document.createElement('source');

      source.setAttribute('src', './orb2/idol.mp4');
      source.setAttribute('type', 'video/mp4');

      vid.innerHTML = "";
      //document.getElementById("content").innerHTML = "whatever";

      vid.appendChild(source);
      vid.play();
      console.log({
        src: source.getAttribute('src'),
        type: source.getAttribute('type'),
      });

      setTimeout(function() {
        vid.pause();

        source.setAttribute('src', './orb2/seeding.mp4');
        source.setAttribute('type', 'video/mp4');

        vid.load();
        vid.play();
        console.log({
          src: source.getAttribute('src'),
          type: source.getAttribute('type'),
        });
      }, 3000);
    }
  }

  videoController.init();

</script>

<button onclick="videoController.playVid()" type="button">播放视频</button>
<button onclick="videoController.pauseVid()" type="button">暂停视频</button><br>

循环
<video id="myVideo" width="320" height="176" onended="videoController.videoEnded()">
  <source src="./orb2/idol.mp4" type="video/mp4">
  您的浏览器不支持HTML5视频。
</video>

<button onClick="videoController.changeMood('Idol')">Idol</button>
<button onClick="videoController.changeMood('Seeding')">Seeding</button>
<button onClick="videoController.changeMood('DeepSearch')">DeepSearch</button>

</body>
</html>

请注意,这是代码部分的翻译,不包括代码中的注释。如果您需要对注释进行翻译,请提供具体的注释内容。

英文:

循环动画HTML5 – 仅在当前视频播放完毕后替换视频

I have the following code where it shows an orb animation on loop. I want to be able to control the changes in animation so that if I click on the seeding/deepsearch buttons it changes the video ONLY after the current animation has finished. Need to include maybe preloading of videos and event bubbling handling to avoid trying to switch different animations whilst one is still playing. Maybe take on the very latest button clicked animation - skip trying to play an earlier click.

https://codesandbox.io/s/fragrant-thunder-czctj7

&lt;!DOCTYPE html&gt; 
&lt;html&gt; 
&lt;body&gt; 
&lt;script&gt; 
let animationStackObject = [{
&quot;state&quot;:&quot;idol&quot;,
&quot;path&quot;: &quot;./orb2/idol.mp4&quot;
},
{
&quot;state&quot;:&quot;seeding&quot;,
&quot;path&quot;: &quot;./orb2/seeding.mp4&quot;
},
{
&quot;state&quot;: &quot;deepsearch&quot;,
&quot;path&quot;: &quot;./orb2/deepsearch.mp4&quot;
}
]
let videoController = {
vid: document.getElementById(&quot;myVideo&quot;),
init: function(){
//let vid = document.getElementById(&quot;myVideo&quot;); 
//vid.src = getAnimation(&quot;idol&quot;, animationStackObject)
//setCurrentState(&quot;idol&quot;);
//this.playVid()
//playVideo()//loopable
},
getAnimation: function(state, animationStackObject){
//-- return animationStackElement of correct state requested
},
playVid: function() {
let vid = document.getElementById(&quot;myVideo&quot;); 
vid.play();
console.log(vid.duration);
console.log(vid.currentTime);
//duration of video in 1 loop is 5 seconds - start a timer
},
pauseVid: function() { 
let vid = document.getElementById(&quot;myVideo&quot;);
vid.pause();  
},
videoEnded: function() {
console.log(&quot;The End.&quot;);
this.playVid()
},
changeMood: function(state){
//is video still playing
//at point video has finished break loop and send new video to be played
//setCurrentState(&quot;idol&quot;);
//change current video to new video path
//play loop
console.log(state);
this.changeVideo();
},
changeVideo: function() {
//
var vid = document.getElementById(&#39;myVideo&#39;);
var source = document.createElement(&#39;source&#39;);
source.setAttribute(&#39;src&#39;, &#39;./orb2/idol.mp4&#39;);
source.setAttribute(&#39;type&#39;, &#39;video/mp4&#39;);
vid.innerHTML = &quot;&quot;;
//document.getElementById(&quot;content&quot;).innerHTML = &quot;whatever&quot;;
vid.appendChild(source);
vid.play();
console.log({
src: source.getAttribute(&#39;src&#39;),
type: source.getAttribute(&#39;type&#39;),
});
setTimeout(function() {
vid.pause();
source.setAttribute(&#39;src&#39;, &#39;./orb2/seeding.mp4&#39;);
source.setAttribute(&#39;type&#39;, &#39;video/mp4&#39;);
vid.load();
vid.play();
console.log({
src: source.getAttribute(&#39;src&#39;),
type: source.getAttribute(&#39;type&#39;),
});
}, 3000);
//
}
}
videoController.init();
&lt;/script&gt; 
&lt;button onclick=&quot;videoController.playVid()&quot; type=&quot;button&quot;&gt;Play Video&lt;/button&gt;
&lt;button onclick=&quot;videoController.pauseVid()&quot; type=&quot;button&quot;&gt;Pause Video&lt;/button&gt;&lt;br&gt; 
loop
&lt;video id=&quot;myVideo&quot; width=&quot;320&quot; height=&quot;176&quot; onended=&quot;videoController.videoEnded()&quot;&gt;
&lt;source src=&quot;./orb2/idol.mp4&quot; type=&quot;video/mp4&quot;&gt;
Your browser does not support HTML5 video.
&lt;/video&gt;
&lt;button onClick=&quot;videoController.changeMood(&#39;Idol&#39;)&quot;&gt;Idol&lt;/button&gt;
&lt;button onClick=&quot;videoController.changeMood(&#39;Seeding&#39;)&quot;&gt;Seeding&lt;/button&gt;
&lt;button onClick=&quot;videoController.changeMood(&#39;DeepSearch&#39;)&quot;&gt;DeepSearch&lt;/button&gt;
&lt;/body&gt; 
&lt;/html&gt;

答案1

得分: 1

将最新的按钮点击保存到一个变量中,并且只在当前视频结束时更改视频源:

let animationStackObject = [
  {
    "state": "idol",
    "path": "https://czctj7.csb.app/orb2/idol.mp4"
  },
  {
    "state": "seeding",
    "path": "https://czctj7.csb.app/orb2/seeding.mp4"
  },
  {
    "state": "deepsearch",
    "path": "https://czctj7.csb.app/orb2/deepsearch.mp4"
  }
];

// 存储下一个要播放的视频。Null 表示继续当前视频。
let next = null;

let videoController = {
  vid: document.getElementById("myVideo"),
  init: function() {
    //let vid = document.getElementById("myVideo"); 
    //vid.src = getAnimation("idol", animationStackObject)

    //setCurrentState("idol");
    //this.playVid()
    //playVideo()//loopable
  },
  getAnimation: function(state, animationStackObject) {
    //-- 返回所请求状态的 animationStackElement
  },
  playVid: function() {
    let vid = document.getElementById("myVideo");
    vid.play();
    console.log(vid.duration);
    console.log(vid.currentTime);
    // 1 次循环内视频的持续时间为 5 秒 - 启动计时器
  },
  pauseVid: function() {
    let vid = document.getElementById("myVideo");
    vid.pause();
  },
  videoEnded: function() {
    console.log("结束。");
    // 如果选择了不同的视频,切换到它。
    if (next) {
      this.changeVideo(next);
      next = null;
    }
    this.playVid()
  },
  changeMood: function(state) {
    // 视频仍在播放吗?

    // 在视频结束时,中断循环并发送新的视频以播放

    // setCurrentState("idol");
    // 将当前视频更改为新的视频路径
    // 播放循环
    console.log(state);
    next = state.toLowerCase();
  },
  changeVideo: function(which) {
    const vid = document.getElementById('myVideo');
    const source = vid.querySelector('source');
    
    source.setAttribute('src', animationStackObject.find(({ state }) => state === which).path);
    
    vid.load();
    vid.play();
    
    console.log({
      src: source.getAttribute('src'),
      type: source.getAttribute('type'),
    });
  }
}

videoController.init();
<button onclick="videoController.playVid()" type="button">播放视频</button>
<button onclick="videoController.pauseVid()" type="button">暂停视频</button><br> 循环
<video id="myVideo" width="320" height="176" onended="videoController.videoEnded()">
  <source src="https://czctj7.csb.app/orb2/idol.mp4" type="video/mp4">
  您的浏览器不支持 HTML5 视频。
</video>

<button onClick="videoController.changeMood('Idol')">偶像</button>
<button onClick="videoController.changeMood('Seeding')">播种</button>
<button onClick="videoController.changeMood('DeepSearch')">深度搜索</button>
英文:

Save the latest button click into a variable, and only change the source when the current video ends:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let animationStackObject = [
{
&quot;state&quot;: &quot;idol&quot;,
&quot;path&quot;: &quot;https://czctj7.csb.app/orb2/idol.mp4&quot;
},
{
&quot;state&quot;: &quot;seeding&quot;,
&quot;path&quot;: &quot;https://czctj7.csb.app/orb2/seeding.mp4&quot;
},
{
&quot;state&quot;: &quot;deepsearch&quot;,
&quot;path&quot;: &quot;https://czctj7.csb.app/orb2/deepsearch.mp4&quot;
}
];
// Store which video will play next. Null means continue the
// current video.
let next = null;
let videoController = {
vid: document.getElementById(&quot;myVideo&quot;),
init: function() {
//let vid = document.getElementById(&quot;myVideo&quot;); 
//vid.src = getAnimation(&quot;idol&quot;, animationStackObject)
//setCurrentState(&quot;idol&quot;);
//this.playVid()
//playVideo()//loopable
},
getAnimation: function(state, animationStackObject) {
//-- return animationStackElement of correct state requested
},
playVid: function() {
let vid = document.getElementById(&quot;myVideo&quot;);
vid.play();
console.log(vid.duration);
console.log(vid.currentTime);
//duration of video in 1 loop is 5 seconds - start a timer
},
pauseVid: function() {
let vid = document.getElementById(&quot;myVideo&quot;);
vid.pause();
},
videoEnded: function() {
console.log(&quot;The End.&quot;);
// If there is a different video selected,
// switch to it.
if (next) {
this.changeVideo(next);
next = null;
}
this.playVid()
},
changeMood: function(state) {
//is video still playing
//at point video has finished break loop and send new video to be played
//setCurrentState(&quot;idol&quot;);
//change current video to new video path
//play loop
console.log(state);
next = state.toLowerCase();
},
changeVideo: function(which) {
const vid = document.getElementById(&#39;myVideo&#39;);
const source = vid.querySelector(&#39;source&#39;);
source.setAttribute(&#39;src&#39;, animationStackObject.find(({ state }) =&gt; state === which).path);
vid.load();
vid.play();
console.log({
src: source.getAttribute(&#39;src&#39;),
type: source.getAttribute(&#39;type&#39;),
});
}
}
videoController.init();

<!-- language: lang-html -->

&lt;button onclick=&quot;videoController.playVid()&quot; type=&quot;button&quot;&gt;Play Video&lt;/button&gt;
&lt;button onclick=&quot;videoController.pauseVid()&quot; type=&quot;button&quot;&gt;Pause Video&lt;/button&gt;&lt;br&gt; loop
&lt;video id=&quot;myVideo&quot; width=&quot;320&quot; height=&quot;176&quot; onended=&quot;videoController.videoEnded()&quot;&gt;
&lt;source src=&quot;https://czctj7.csb.app/orb2/idol.mp4&quot; type=&quot;video/mp4&quot;&gt;
Your browser does not support HTML5 video.
&lt;/video&gt;
&lt;button onClick=&quot;videoController.changeMood(&#39;Idol&#39;)&quot;&gt;Idol&lt;/button&gt;
&lt;button onClick=&quot;videoController.changeMood(&#39;Seeding&#39;)&quot;&gt;Seeding&lt;/button&gt;
&lt;button onClick=&quot;videoController.changeMood(&#39;DeepSearch&#39;)&quot;&gt;DeepSearch&lt;/button&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月26日 09:37:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定