Last active
January 23, 2016 13:17
-
-
Save PonDad/f00ac29a653ff931399f to your computer and use it in GitHub Desktop.
ブラウザで音声操作をする。(Speech Recognition API) ref: http://qiita.com/PonDad/items/4ca433ad03efbf7499a2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<textarea id="textarea" rows=10 cols=80></textarea> | |
<button id="button" onclick="toggleStartStop()"></button> | |
<script type="text/javascript"> | |
var recognizing; | |
var recognition = new SpeechRecognition(); | |
recognition.continuous = true; | |
reset(); | |
recognition.onend = reset; | |
recognition.onresult = function (event) { | |
for (var i = resultIndex; i < event.results.length; ++i) { | |
if (event.results.final) { | |
textarea.value += event.results[i][0].transcript; | |
} | |
} | |
} | |
function reset() { | |
recognizing = false; | |
button.innerHTML = "Click to Speak"; | |
} | |
function toggleStartStop() { | |
if (recognizing) { | |
recognition.stop(); | |
reset(); | |
} else { | |
recognition.start(); | |
recognizing = true; | |
button.innerHTML = "Click to Stop"; | |
} | |
} | |
</script> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<head lang="ja-JP"> | |
<meta charset="utf-8"> | |
<title>HTML5 ボイスコントロール テスト</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" type="text/css" /> | |
<link href="style.css" rel="stylesheet"> | |
</head> | |
<body> | |
<h1 class="text-center h3"> Speech Recognition API 6</h1> | |
<p class="text-center">ボイスコントロールテスト</p> | |
<div id="content"> | |
<div id="box"> | |
</div> | |
</div> | |
<div class="text-center"> | |
<button id="startRecBtn" class="btn btn-default">マイク 開始</button> | |
<button id="stopRecBtn" class="btn btn-default">マイク 停止</button> | |
</div> | |
<div id="text" class="text-center"> | |
「赤」「青」「黄色」「緑」<br> | |
って話しかけてみてね。 | |
</div> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script> | |
<script src="main.js"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
var rec = new webkitSpeechRecognition(); | |
rec.continuous = true; | |
rec.interimResults = false; | |
rec.lang = 'ja-JP'; | |
var userSaid = function(str, s) { | |
return str.indexOf(s) > -1; | |
} | |
rec.onresult = function(e) { | |
for (var i = e.resultIndex; i < e.results.length; ++i) { | |
if (e.results[i].isFinal) { | |
var str = e.results[i][0].transcript; | |
console.log('Recognised: ' + str); | |
if (userSaid(str, '赤')) { | |
$('#box').css("background-color","red"); | |
} | |
else if (userSaid(str, '青')) { | |
$('#box').css("background-color","blue"); | |
} | |
else if (userSaid(str, '黄色')) { | |
$('#box').css("background-color","yellow"); | |
} | |
else if (userSaid(str, '緑')) { | |
$('#box').css("background-color","green"); | |
} | |
} | |
} | |
} | |
$("#startRecBtn").click(function() { | |
rec.start(); | |
}); | |
$("#stopRecBtn").click(function() { | |
rec.stop(); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import url(//fonts.googleapis.com/earlyaccess/notosansjapanese.css); | |
body { | |
font-family: 'Noto Sans Japanese', serif; | |
max-width:640px; | |
margin: 0 auto; | |
} | |
#content{ | |
margin: 0 auto; | |
width:300px; | |
height: 200px; | |
padding:20px; | |
position: relative; | |
} | |
#box{ | |
position: absolute; | |
top: 75px; | |
right:125px; | |
width:50px; | |
height:50px; | |
background-color:#ddd; | |
} | |
#text{ | |
margin-top:40px; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment