1 <!doctype html> 2 <meta charset="utf-8"> 3 <title> Web Speech Recognition </title> 4 5 <script> 6 var successes = 0; 7 var hash = window.location.hash; 8 var recognizer = new webkitSpeechRecognition(); 9 10 (function () { 11 12 switch (hash) { 13 // Just probe if creating a SpeechRecognition object worked. 14 case "#precheck": 15 notify(recognizer == null ? 'fail' : 'success'); 16 return; 17 18 case "#oneshot": 19 recognizer.continuous = false; 20 break; 21 22 case "#continuous": 23 recognizer.continuous = true; 24 break; 25 26 default: 27 return; 28 } 29 30 recognizer.onresult = function(e) { 31 var value = e.results[e.resultIndex][0].transcript; 32 if (value == 'Pictures of the moon') { 33 successes++; 34 notify('goodresult' + successes); 35 } else { 36 notify('badresult'); 37 } 38 } 39 recognizer.onerror = function(e) { 40 console.log('error', e); 41 notify('error' + e.error); 42 } 43 recognizer.onnomatch = function() { console.log('nomatch'); } 44 recognizer.onaudiostart = function() { console.log('audiostart'); } 45 recognizer.onsoundstart = function() { console.log('soundstart'); } 46 recognizer.onsoundend = function() { console.log('soundend'); } 47 recognizer.onaudioend = function() { console.log('audioend'); } 48 49 recognizer.start(); 50 51 })(); 52 53 function notify(status) { 54 document.location = '#' + status; 55 document.location.reload(true); 56 } 57 </script> 58