当用户在启用了 Youtube 历史记录的情况下登录 YouTube 时,开始时间将不起作用。
不知何故,视频播放后youtube会转到历史上最后一个保存时间。
重现步骤
预期结果:
实际结果:
<html>
<script src="https://www.youtube.com/iframe_api" type="application/javascript"></script>
<script>
let player;
function onYouTubeIframeAPIReady() {
console.log('ready')
player = new YT.Player('player', {
sandbox: true,
height: '390',
width: '640',
videoId: 'T9oT-oOaTNU',
playerVars: {
'playsinline': 1,
'autoplay': 0,
'start': 1500
},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
player.seekTo = (n) => {
console.log({ n });
}
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
//event.target.playVideo();
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
let done = false;
function onPlayerStateChange(event) {
}
function stopVideo() {
player.stopVideo();
}
</script>
<body>
<div class="responsive-embed widescreen">
<div class="embed video-player">
<div id="player"></div>
</div>
</div>
</body>
</html>
我通过将 IFrame 域更改为 www.youtube-nocookie.com 而不是 www.youtube.com 解决了这个问题:
<iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/VIDEO_ID?fs=1&start=START_TIME&end=END_TIME" frameborder="0" allowfullscreen></iframe>
将 VIDEO_ID 更改为您的视频 ID,将 START_TIME 更改为您想要的开始时间,将 END_TIME 更改为结束时间,或者在我的情况下更改为开始时间。
一些背景故事: 上个月我的网站突然遇到了这个问题,以前没有发生过。
我试图向 Google 这里提交一个错误,但他们说他们无法重现它,并表示这似乎是一个浏览器问题,这与 this 2015 年的过时帖子相匹配