首页 > Web开发 > 详细

网页内嵌YouTube视频,极其简单

时间:2021-08-16 14:44:37      阅读:88      评论:0      收藏:0      [点我收藏+]

1,在YouTube中复制iframe代码

技术分享图片

2,HTML代码:

 1 <!DOCTYPE html>
 2 <html>
 3   <body>
 4     <!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
 5     <div id="player"></div>
 6 
 7     <script>
 8       // 2. This code loads the IFrame Player API code asynchronously.
 9       var tag = document.createElement(script);
10 
11       tag.src = "https://www.youtube.com/iframe_api";
12       var firstScriptTag = document.getElementsByTagName(script)[0];
13       firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
14 
15       // 3. This function creates an <iframe> (and YouTube player)
16       //    after the API code downloads.
17       var player;
18       function onYouTubeIframeAPIReady() {
19         player = new YT.Player(player, {
20           height: 360,
21           width: 640,
22           videoId: M7lc1UVf-VE,
23           events: {
24             onReady: onPlayerReady,
25             onStateChange: onPlayerStateChange
26           }
27         });
28       }
29 
30       // 4. The API will call this function when the video player is ready.
31       function onPlayerReady(event) {
32         event.target.playVideo();
33       }
34 
35       // 5. The API calls this function when the player‘s state changes.
36       //    The function indicates that when playing a video (state=1),
37       //    the player should play for six seconds and then stop.
38       var done = false;
39       function onPlayerStateChange(event) {
40         if (event.data == YT.PlayerState.PLAYING && !done) {
41           setTimeout(stopVideo, 6000);
42           done = true;
43         }
44       }
45       function stopVideo() {
46         player.stopVideo();
47       }
48     </script>
49   </body>
50 </html>

帮助文档:https://developers.google.com/youtube/iframe_api_reference#setPlaybackQuality

网页内嵌YouTube视频,极其简单

原文:https://www.cnblogs.com/smartisn/p/15146762.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!