javascript videoタグが繰り返し再生になっているかを判定する

javascript videoタグが繰り返し再生になっているかを判定する

javascriptで、videoタグが繰り返し再生になっているかを判定するサンプルコードを記述してます。「video」タグに「loop」プロパティが設定されているかを確認して判定します。

環境

  • OS windows11 home
  • ブラウザ chrome 108.0.5359.99

繰り返し再生になっているかを判定

繰り返し再生になっているかを判定するには、「loop」プロパティを確認します。

<video id="video" loop muted autoplay style="width:700px;height:400px;">
    <source src="test.mp4">
</video>

<video id="video2" style="width:700px;height:400px;">
    <source src="test.mp4">
</video>

<script>

console.log(
	document.getElementById('video').loop // true
)

console.log(
	document.getElementById('video2').loop // false
)

</script>

判定されていることが確認できます。

また、javascript部はdocument.getElementByIdを省略して記述することも可能です。

console.log(
	video.loop
)

console.log(
	video2.loop
)

サンプルコード

以下は、ボタンをクリックすると、繰り返し再生を設定するサンプルコードとなります。

※cssには「Material Design for Bootstrap」を使用してます。関数はアロー関数を使用してます。

<!DOCTYPE html>
<html lang="ja">

<head>
  <meta charset="utf-8">
  <title>mebeeサンプル</title>
  <!-- Font Awesome -->
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet" />
  <!-- Google Fonts -->
  <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" />
  <!-- MDB -->
  <link href="https://cdnjs.cloudflare.com/ajax/libs/mdb-ui-kit/4.1.0/mdb.min.css" rel="stylesheet" />
</head>
<style>
  .main {
    margin: 0 auto;
    margin-top: 150px;
    display: flex;
    flex-direction: column;
    align-items: center;
    font-size: 20px;
  }
</style>
<script>

  const hoge = () => {
    
    video.loop = true

  }

</script>

<body>
  <div class="main">

    <video id="video" autoplay muted style="width:700px;height:400px;">
      <source src="test.mp4">
    </video>

    <button type="button" class="btn btn-raised btn-danger mt-3" onclick="hoge();">設定</button>

</body>

</html>

設定されていることが確認できます。