关于嵌入式视频设置播放结束后自动重播的技术咨询
Hey there! Let's figure out how to get your embedded YouTube video to replay automatically when it ends.
First, looking at your existing code, I notice you already added loop=1 to your iframe's URL parameters—but it might not be working because YouTube's loop parameter requires the playlist parameter to be set to the same video ID for single-video loops to work. That's a common gotcha!
Also, there's a small bug in your iframe code: you accidentally duplicated the closing part of the iframe tag at the end, which could cause rendering issues. Let's fix that up too.
Here's the adjusted code that should make your video loop properly:
if (item.getAttribute("value").includes('http')) { // Fixed iframe URL with playlist parameter, and removed duplicate closing code document.getElementById('player').innerHTML = `<iframe width="1179" height="663" src="https://www.youtube.com/embed/668nUCeBHyY?autoplay=1&cc_load_policy=1&controls=0&disablekb=1&fs=0&iv_load_policy=3&loop=1&autopause=0&playlist=668nUCeBHyY" frameborder="0" allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>`; console.log("I'm here in the if block"); const video = document.getElementById('player'); }
Let me break down the key fixes:
- Added
&playlist=668nUCeBHyYto the iframe URL: This tells YouTube to treat the single video as a playlist, which makes theloop=1parameter actually trigger a replay. - Removed the duplicate
errerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>part at the end of your iframe code—this was causing invalid HTML that might have broken the player.
If you ever need more control (like triggering custom actions when the video ends), you could use the YouTube IFrame API to listen for the onStateChange event, but for simple auto-replay, the parameter fix above is the easiest way to go.
Hope that gets your video looping smoothly!
备注:内容来源于stack exchange,提问作者AbderaoufLaib

