単語帳アプリをJSとHTMLで実装しています。
その中でボタンを押すと音声ファイルを再生するプログラムにplay()関数を使おうと思ったのですが使い方がわかりません。
指定していなくても使えるとネットでは書いていたのですがエラーメッセージが出ていました。
Uncaught ReferenceError: play is not defined
どうすれば該当音声URLまたはFileを再生できるでしょうか。
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <title>Flash Cards</title> <style> body { margin: 0; background: #e0e0e0; text-align: center; font-family: Verdana, sans-serif; color: #fff; } #btn { width: 200px; margin: 0 auto; padding: 7px; border-radius: 5px; background: #00aaff; box-shadow: 0 4px 0 #0088cc; cursor: pointer; } #btn:hover { opacity: 0.8; } #voice { margin: 30px auto 20px; width: 50px; height: 25px padding: 7px; font-size: 15px; line-height: 25px; border-radius: 5px; background: #00aaff; cursor: pointer; } #voice:hover { opacity: 0.8; } #card { margin: 60px auto 20px; width: 550px; height: 100px; cursor: pointer; font-size: 38px; line-height: 100px; perspective: 100px; transform-style: preserve-3d; transition: transform .8s; } #card-front, #card-back { display: block; width: 100%; height: 100%; border-radius: 5px; position: absolute; backface-visibility: hidden; } #card-front { background: #fff; color: #333; } #card-back { background: #00aaff; transform: rotateY(180deg); } #card-voice { background: #00aaff; transform: rotateY(180deg); } .open{ transform: rotateY(180deg); } </style> </head> <body> <div id="card"> <div id="card-front"></div> <div id="card-back"></div> </div> <div id="voice" onclick="play()">voice</div> <div id="btn">NEXT</div> <script> (function(){ 'use strict'; var words = [ {'en': 'all show and no go', 'ja': '見てくれだけの役立たず','voice':'http://www.dotup.org/uploda/www.dotup.org1239946.m4a.html' }, {'en': 'authenticity', 'ja': '信頼性','voice':'http://www.dotup.org/uploda/www.dotup.org1239947.m4a.html' }, {'en': 'be in seventh heaven', 'ja': '有頂天である','voice':'http://www.dotup.org/uploda/www.dotup.org1239949.m4a.html' }, {'en': 'gateway to success', 'ja': '登竜門','voice':'http://www.dotup.org/uploda/www.dotup.org1239952.m4a.html' }, {'en': 'hen-pecked husband', 'ja': '奥さんに頭が上がらないダンナ','voice':'http://www.dotup.org/uploda/www.dotup.org1239953.m4a.html' }, {'en': 'I will eat my hat if 〜', 'ja': '〜のことは絶対にない','voice':'http://www.dotup.org/uploda/www.dotup.org1239954.m4a.html' }, {'en': 'I will game!', 'ja': '私、行きます','voice':'http://www.dotup.org/uploda/www.dotup.org1239957.m4a.html' } ]; var card =document.getElementById('card'); var cardFront =document.getElementById('card-front'); var cardBack =document.getElementById('card-back'); var cardVoice =document.getElementById('card-voice'); var btn =document.getElementById('btn'); var voice =document.getElementById('voice'); card.addEventListener('click',function(){ flip(); }); btn.addEventListener('click', function(){ next(); }); function next(){ if(card.className === 'open'){ card.addEventListener('transitionend',setCard); flip(); }else{ setCard(); } } function setCard(){ var num = Math.floor(Math.random() * words.length); cardFront.innerHTML = words[num]['en']; cardBack.innerHTML = words[num]['ja']; voice = new Audio(words[num]['voice']); card.removeEventListener('transitionend',setCard); } setCard(); function flip(){ card.className = card.className === '' ? 'open' : ''; } })(); </script> </body> </html>
あなたの回答
tips
プレビュー