サイトにyoutubeを埋め込みたいのですが、オートプレイで、音無し、さらに動画サイズは横幅100%で表示したいのですが、動画サイズの調整でうまくいきません。
解決策を教えてください。
・表示結果
https://test2.ocnk.net/
・こちらのベストアンサーを参考にしたhtmlです。
https://teratail.com/questions/86904
html
1<!DOCTYPE html> 2<html> 3 <head> 4 <meta charset="utf-8"/> 5 </head> 6 <body> 7 <!-- 1. The <iframe> (and video player) will replace this <div> tag. --> 8 <div id="player"></div> 9 10 <script> 11 // 2. This code loads the IFrame Player API code asynchronously. 12 var tag = document.createElement('script'); 13 14 tag.src = "https://www.youtube.com/iframe_api"; 15 var firstScriptTag = document.getElementsByTagName('script')[0]; 16 firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); 17 18 // 3. This function creates an <iframe> (and YouTube player) 19 // after the API code downloads. 20 var player; 21 22 // 動画のID 23 const videoId = 'SJaNY_iJ0XE'; 24 25 function onYouTubeIframeAPIReady() { 26 player = new YT.Player('player', { 27 height: '360', 28 width: '640', 29 playerVars: { 30 playlist: videoId, 31 // 以下のオプションはChrome・Firefox・Operaでは使えないが、なぜかEdgeでは使える 32 autoplay: 1, 33 mute: 1, 34 loop: 1 35 }, 36 videoId: videoId, 37 // イベントがEdgeで発火しない・・・? 38 events: { 39 'onReady': onPlayerReady, 40 'onStateChange': onPlayerStateChange 41 } 42 }); 43 } 44 45 // 4. The API will call this function when the video player is ready. 46 // ↑ 実際はプレイリストを読み込む場合は準備できていない。少し待たないといけない。 47 function onPlayerReady(event) { 48 event.target.mute(); // ミュート 49 event.target.setLoop(true); // ループ 50 event.target.playVideo(); // 動画の開始 51 } 52 53 // 5. The API calls this function when the player's state changes. 54 // The function indicates that when playing a video (state=1), 55 // the player should play for six seconds and then stop. 56 function onPlayerStateChange(event) { 57 } 58 </script> 59 </body> 60</html>

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。