乐闻世界logo
搜索文章和话题

How to make an embedded Youtube video automatically start playing?

1个答案

1

To enable auto-play for embedded YouTube videos, add a specific parameter to the embed code. Specifically, include the autoplay=1 parameter in the video URL.

For example, consider the standard embedded YouTube video code below:

html
<iframe width="560" height="315" src="https://www.youtube.com/embed/M7lc1UVf-VE" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

To enable auto-play for this video, modify the src attribute and include autoplay=1. The modified code is:

html
<iframe width="560" height="315" src="https://www.youtube.com/embed/M7lc1UVf-VE?autoplay=1" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

Note that when using the autoplay parameter, to adhere to browser policies, you may need to mute the video, as many browsers restrict auto-play for videos with sound. To mute the video, add the mute=1 parameter:

html
<iframe width="560" height="315" src="https://www.youtube.com/embed/M7lc1UVf-VE?autoplay=1&mute=1" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

This modified embed code will allow the video to auto-play upon loading without violating browser auto-play policies.

2024年8月15日 01:05 回复

你的答案