To embed a YouTube video in an HTML document, you can use the YouTube embed code or the YouTube Player API. Here's how you can do it:
1. Using YouTube Embed Code:
- Go to the YouTube video you want to embed.
- Click on the "Share" button below the video.
- Click on the "Embed" option.
- Customize the video size, playback options, and other settings if desired.
- Copy the generated HTML code.
- Paste the HTML code into your HTML document where you want the video to appear.
The generated code will look similar to this:
html
<iframe width="560" height="315" src="https://www.youtube.com/embed/VIDEO_ID"
frameborder="0" allowfullscreen></iframe>
Replace "VIDEO_ID" with the actual YouTube video ID, which is a unique identifier for each video.
2. Using YouTube Player API:
- Include the YouTube Player API JavaScript library in your HTML document. Add the following script tag in the head section of your HTML:
html
<script src="https://www.youtube.com/player_api"></script>
- Create a container element (e.g., a div) in your HTML where you want the video player to be placed:
html
<div id="player"></div>
- Add a JavaScript code block to initialize the YouTube player and specify the video to be played:
html
<script>
// This code loads the YouTube IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// This function creates an <iframe> (and YouTube player) after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '315',
width: '560',
videoId: 'VIDEO_ID',
});
}
</script>
Replace "VIDEO_ID" with the actual YouTube video ID.
Note: The YouTube Player API allows you to have more control over the player, such as controlling playback, events, and customizing the player's appearance. You can refer to the YouTube Player API documentation for more advanced usage.
Remember to replace "VIDEO_ID" in both methods with the actual YouTube video ID you want to embed.
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc