0
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?

htmlでの音声再生のパターン

Posted at

ネットで調べたり、チャットGPTに書かせて上手く行ったコードをメモ

クイズボタン

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>クイズボタン音声</title>
    <style>
        .btn a {
            width: 150px;
            height: 50px;
            line-height: 50px;
            text-align: center;
            display: block;
            background: #999;
            border-radius: 5px;
            border-bottom: 4px solid #666;
            color: #FFF;
            margin: 0 auto;
            text-decoration: none;
            font-size: 16px;
            font-weight: bold;
        }
        .btn a:active {
            background: #777;
            border-bottom: 2px solid #444;
        }
    </style>
</head>
<body>

    <p class="btn"><a href="#" id="playSound1">正解</a></p>
    <p class="btn"><a href="#" id="playSound2">不正解</a></p>

    <audio id="sound1" preload="auto">
        <source src="正解.wav" type="audio/wav">
    </audio>

    <audio id="sound2" preload="auto">
        <source src="不正解.mp3" type="audio/mp3">
        <source src="不正解.wav" type="audio/wav">
    </audio>


    <script>
        document.getElementById('playSound1').addEventListener('click', function(event) {
            event.preventDefault(); // ページのジャンプを防ぐ
            var sound = document.getElementById('sound1');
            sound.currentTime = 0; // 再生位置をリセット
            sound.play();
        });
        document.getElementById('playSound2').addEventListener('click', function(event) {
            event.preventDefault(); // ページのジャンプを防ぐ
            var sound = document.getElementById('sound2');
            sound.currentTime = 0; // 再生位置をリセット
            sound.play();
        });
    </script>

</body>
</html>

文字の読み上げ

<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>Javascriptでクイズ作成</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script>
var mondaibun = `
バカが踊って動画を投稿するアプリ,ティックトック,a,b,c
民度の低いコメントをする人,ヤフコメ民,a,b,c
世界一小さい賃貸物件,シルバニアファミリー,a,b,c
`;

var mondai = null;
var sound = [
   "folder3/seikai.mp3",
   "folder3/huseikai.mp3",
   "folder3/start.mp3"
];
sound = sound.map(function(v) {
   var audio = new Audio(v);
   audio.load();
   return audio;
});

function start() {
   $("#start").after('<p id="info"></p><div id="sentaku"></div><hr>');
   $("#start").remove();
   sound[2].play();
   mondai = mondaibun.trim().split("\n");
   setTimeout(function() {
      next();
   }, 200);
}

function next() {
   $("#sentaku").empty();
   if (mondai.length == 0) {
      $("#info").text("終わり");
      return;
   }
   var arr = mondai.shift().split(",");
   var str = arr.shift();
   speechSynthesis.speak(new SpeechSynthesisUtterance("問題。" + str));
   $("#info").text("問題 :" + str);
   var kotae = arr[0];
   arr = _.shuffle(arr);
   arr.forEach(function(v, i) {
      var flag = (kotae == v) ? true : false;
      var html = `<p>${i+1}: <input type="button" value="${v}" onclick="sentaku(${flag})"></p>`;
      $("#sentaku").append(html);
   });
}

function sentaku(x) {
   if (x == true) {
      speechSynthesis.cancel();
      sound[0].currentTime = 0;
      sound[0].play();
      setTimeout(function() {
         next();
      }, 800);
   } else {
      sound[1].currentTime = 0;
      sound[1].play();
   }
}
</script>
</head>
<body>
   <h1>Javascriptでクイズ作成</h1>
   <h3>Web Speech APIを利用したクイズです。 対応ブラウザ: Chrome</h3>
   <hr>
   <button id="start" onclick="start()">スタート</button>
</body>
</html>

Soundを普通に再生サンプル

<div>
    <h2>雫が落ちる音</h2>
    <audio controls src="https://res.cloudinary.com/code-kitchen/video/upload/v1555038697/posts/zk5sldkxuebny7mwlhh3.mp3"></audio>
  </div>
0
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
0
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?