5
7

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.

HTML5 音声付き4択クイズを作ってみた

Posted at

デモ:http://pie.karou.jp/zd.html

Web Speech APIで問題文を読んでもらいます

speechSynthesis.speak(new SpeechSynthesisUtterance("問題 1: 問題文"));
<!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 = `
日本で2番目に面積の広い都道府県はどこでしょう,岩手県,a,b,c
日本で2番目に大きな湖はどこでしょう,霞ヶ浦,a,b,c
日本で2番目に長い川はどこでしょう,利根川,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>
5
7
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
5
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?