如何在本地chrome上激活麦克风

时间:2018-08-29 10:10:55

标签: javascript node.js

我有一个在服务器上运行的JavaScript代码,需要访问麦克风,
我单击了链接栏右侧的图标,但没有麦克风,因此我进入了站点设置,并且“允许/全部”列表不起作用!

html代码:

<p id="e">
<div id="result"></div>
<button id="speak">Start record</button>                    

js代码:

var div = document.getElementById("result");

document.getElementById("speak").onclick = function(){
  speechRs.rec_start('en-IN',function(final_transcript,interim_transcript){
       div.innerText = final_transcript+interim_transcript;
  }); 

  speechRs.on("I am fine",function(){   
      alert("user spoken I am fine");
  }); 

}

var speechRs=speechRs||{};speechRs.speechinit=function(lang,cb,bcolor,color,pitch,rate){this.speaker=new SpeechSynthesisUtterance();this.speaker.pitch=pitch||1;this.speaker.rate=rate||1;this.lan=lang;var style=document.createElement('style');style.type='text/css';style.innerHTML='.rsClass{background-color:'+(bcolor||"#4f91e6")+';color:'+(color||"#fff")+';}';document.getElementsByTagName('head')[0].appendChild(style);setTimeout(function(){speechRs.speaker.voice=speechSynthesis.getVoices().filter(function(voice){return voice.name==speechRs.lan;})[0];},500);if(lang=='native'){cb(this);}else{setTimeout(function(){cb(speechRs)},1000);}}
speechRs.speak=function(text,cb,isHiligh){let j=0,el,ar=[];speechRs.speaker.voice=speechSynthesis.getVoices().filter(function(voice){return voice.name==speechRs.lan;})[0];this.speaker.onend=function(e){cb(e);};if(typeof text=='string'){this.speaker.text=text;speechSynthesis.speak(this.speaker);}else{if(isHiligh){j=0;el=text;ar=(text.innerHTML).split(".");readop(ar[j]);}else{this.speaker.text=text.innerHTML;speechSynthesis.speak(this.speaker);}}
function readop(x){speechRs.speaker.text=x;if(j!=0){el.querySelector(".rsClass").className="";}
el.innerHTML=(el.innerHTML).replace(ar[j],"<span class='rsClass'>"+ar[j]+"</span>");speechSynthesis.speak(speechRs.speaker);speechRs.speaker.onend=function(e){if(ar.length>(j+1)){readop(ar[++j]);}}}}
speechRs.rec_start=function(l,callback){this.recognition=new webkitSpeechRecognition();this.recognition.continuous=true;this.recognition.interimResults=true;this.arry_com={};this.final_transcript='';this.recognition.lang=l;this.recognition.start();this.ignore_onend=false;this.recognition.onstart=function(c){}
let prev_res='';this.recognition.onresult=function(event){let interim_transcript='';if(typeof(event.results)=='undefined'){speechRs.recognition.onend=null;speechRs.recognition.stop();return;}
for(var i=event.resultIndex;i<event.results.length;++i){if(event.results[i].isFinal){prev_res='';speechRs.final_transcript+=event.results[i][0].transcript;}else{interim_transcript+=event.results[i][0].transcript;}}
console.log(prev_res+","+interim_transcript);if(prev_res!=interim_transcript&&speechRs.arry_com[interim_transcript.toLowerCase().trim()]){prev_res=interim_transcript;speechRs.arry_com[interim_transcript.toLowerCase().trim()]();}else{}
callback(speechRs.final_transcript.replace("undefined",""),interim_transcript);}}
speechRs.on=function(s,f){this.arry_com[s.toLowerCase()]=f;}
speechRs.rec_stop=function(callback){this.recognition.stop();this.recognition.onstop=function(){return callback();}}

0 个答案:

没有答案