Created
August 15, 2017 08:01
-
-
Save xx7y7xx/c02e55e38dff6bb028dbb4418be00d02 to your computer and use it in GitHub Desktop.
创建session
This file contains 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
var fcVoice = function (voiceCallback) { | |
/* 标识麦克风按钮状态,按下状态值为true,否则为false */ | |
var mic_pressed = false; | |
/** | |
* 初始化Session会话 | |
*/ | |
var session = new IFlyIatSession({ | |
"callback": { | |
"onResult": function (err, result) { | |
/* 若回调的err为空或错误码为0,则会话成功,可提取识别结果进行显示*/ | |
if (err == null || err == undefined || err == 0) { | |
if (result == '' || result == null) | |
console.log("没有获取到识别结果"); | |
else | |
console.log(result); | |
if (voiceCallback) { | |
voiceCallback(result); | |
} | |
/* 若回调的err不为空且错误码不为0,则会话失败,可提取错误码 */ | |
} else { | |
console.log('error code : ' + err + ", error description : " + result); | |
} | |
mic_pressed = false; | |
console.log('end'); | |
}, | |
"onVolume": function (volume) { | |
}, | |
"onError": function () { | |
mic_pressed = false; | |
}, | |
"onProcess": function (status) { | |
switch (status) { | |
case 'onStart': | |
console.log("服务初始化..."); | |
break; | |
case 'normalVolume': | |
case 'started': | |
console.log("倾听中..."); | |
break; | |
case 'onStop': | |
console.log("等待结果..."); | |
break; | |
case 'onEnd': | |
console.log('end.'); | |
break; | |
case 'lowVolume': | |
console.log("倾听中...(声音过小)"); | |
break; | |
default: | |
console.log(status); | |
} | |
} | |
} | |
}); | |
if (!session.isSupport()) { | |
console.log("当前浏览器不支持!"); | |
return; | |
} | |
var play = function () { | |
if (!mic_pressed) { | |
var ssb_param = { | |
"grammar_list": null, | |
"params": "appid=58f45210,appidkey=4da080c4474a1986, lang = sms, acous = anhui, aue=speex-wb;-1, usr = mkchen, ssm = 1, sub = iat, net_type = wifi, rse = utf8, ent =sms16k, rst = plain, auf = audio/L16;rate=16000, vad_enable = 1, vad_timeout = 5000, vad_speech_tail = 500, compress = igzip" | |
}; | |
/* 调用开始录音接口,通过function(volume)和function(err, obj)回调音量和识别结果 */ | |
session.start(ssb_param); | |
mic_pressed = true; | |
} | |
else { | |
//停止麦克风录音,仍会返回已传录音的识别结果. | |
session.stop(); | |
} | |
} | |
/** | |
* 取消本次会话识别 | |
*/ | |
var cancel = function () { | |
session.cancel(); | |
} | |
fcVoice.start = function () { | |
play(); | |
console.log('start'); | |
} | |
fcVoice.end = function () { | |
session.cancel(); | |
} | |
//页面不可见,断开麦克风调用 | |
document.addEventListener("visibilitychange", function () { | |
if (document.hidden == true) { | |
session.kill(); | |
} | |
}); | |
return session; | |
} | |
function fcVoiceInit(voiceCallback) { | |
return fcVoice(voiceCallback); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment