1
2

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 3 years have passed since last update.

HTML & JavaScriptで複数動画を順次ループ再生する方法

Posted at

完成コード
ハマった点1)自動再生しない
ハマった点2)ビデオがフッタの下に表示された
参考記事

#完成コード

<div class="top-video">
<video id='video' width="640" height="360" autoplay muted ></video>
<script type="text/javascript">
'use strict';

var playlist = [
  '<ビデオ1URL>.mp4',
  '<ビデオ2URL>.mp4',
  '<ビデオ3URL>.mp4'
]

var video = document.getElementById('video');
video.muted = true;
video.autoplay = true;
video.style.width = '640';
video.style.height = '360';
video.controls = false;
video.src = playlist[0];
video.play();
var index = 0;

window.addEventListener('DOMContentLoaded', function(){
const videoElement = document.querySelector("video");
videoElement.muted = true;
videoElement.autoplay = true;
});

video.addEventListener('ended', function(){
  index++;
  if (index < playlist.length) {
    video.src = playlist[index];
    video.play();
  }
  else {
    video.src = playlist[0];
    video.play();
    index = 0;
  }
});
</script>
</div>

#ハマった点1)自動再生しない

HTMLにautoplayを指定したのに自動再生しなかった

<video id='video' width="640" height="360" autoplay></video>

最近のブラウザでは、音声付き動画の自動再生が制限されているため
対策:
muted:無音声再生を指定すれば自動再生された

<video id='video' width="640" height="360" autoplay muted></video>

#ハマった点2)ビデオがフッターの下に表示された

document.body.appendChild(video);

このコードのために、すべての要素の最後に回されていた
削除して普通にコンテンツ領域に表示


#参考記事
https://gist.github.com/yoshiiz/6134482f8fd72063e7221b7b82d27e67
http://lifelog.main.jp/wordpress/?p=981
https://se.ekaki-j.com/html5-video/

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?