YouTube IFrame API在结束前停止

时间:2016-03-16 09:26:19

标签: youtube-api

是否可以在结束前一秒钟使用YouTube API停止视频,或者只是停止重置回到开头?这与视频本身的长度无关。

提前致谢

1 个答案:

答案 0 :(得分:1)

I disagree with the argument of NOT being able to stop a video before it ends. Specifically, in the API, you have access to:

https://developers.google.com/youtube/js_api_reference#Playback_status

Player.getCurrentTime();
Player.getDuration();

If you were to monitor the player object and compare these two, you could easily stop the video before it ends. An example would be (although probably not good in production):

setInterval(function() {
  var player = getPlayer(); //retrieve the player object

  if(player) {
   var duration = player.getDuration();
   var current  = player.getCurrentTime();


   if((duration - current) <= 1) {
     player.stopVideo();
   }
 }
}, 1000); //every second