4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

YouTubeをページを開いた瞬間に再生させる方法(2019/5/14現在)

Last updated at Posted at 2019-05-14

##はじめに
Webサービスの基本はメディアサイトだと思っていて、おそらく世界最大であろう下記のサイトを見てタイムリーに動画再生をさせる方法が気になった。

「CNN」
https://edition.cnn.com/

##結論
下記のコードになります

sample.html
<!DOCTYPE html>
<html>
  <body>
    <!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
    <div id="player"></div>

    <script>
      var tag = document.createElement('script');

      tag.src = "https://www.youtube.com/iframe_api";
      var firstScriptTag = document.getElementsByTagName('script')[0];
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

      var player;
      function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
          height: '360',
          width: '640',
          videoId: 'M7lc1UVf-VE',
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
          }
        });
      }
      function onPlayerReady(event) {
        event.target.playVideo();
      }
      var done = false;
      function onPlayerStateChange(event) {
        
      }  
    </script>
  </body>
</html>

##諸々
スマホのブラウザではいかなる方法でもページを開いた瞬間に再生させることはできない。
videoタグでもできるとの情報がありましたができないようです。

##参考
やっぱり公式ドキュメントが最強ですね

iframe 組み込みの YouTube Player API リファレンス
https://developers.google.com/youtube/iframe_api_reference?hl=ja

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?