speech常见问题我要提意见

Speech模块管理语音输入功能,提供语音识别功能,可支持用户通过麦克风设备进行语音输入内容。通过plus.speech可获取语音输入管理对象。

语音输入接口可使得网页开发人员能快速调用设备的麦克风进行语音输入,而不需要安装额外的浏览器插件。规范不定义底层语音识别引擎的技术架构,浏览器实现可基于语音识别服务器或本地内置语音识别模块。

方法:

对象:

回调方法:

权限:

permissions


{
// ...
"permissions":{
	// ...
	"Speech": {
		"description": "语音输入"
	}
}
}
			

startRecognize

启动语音识别


void plus.speech.startRecognize( options, successCB, errorCB );
				

说明:

启动语音识别时调用,当语音识别成功后通过successCallback回调返回识别出文本内容,调用语音识别失败则通过errorCallback回调返回。

参数:

返回值:

void : 无

示例:


<!DOCTYPE html>
<html>
	<head>
	<meta charset="utf-8"/>
	<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
	<title>Speech Example</title>
	<script type="text/javascript">
// 监听plusready事件  
document.addEventListener("plusready", function(){
	// 扩展API加载完毕,现在可以正常调用扩展API
	// ...
}, false);
var text=null;
function startRecognize(){
	var options = {};
	options.engine = 'iFly';
	text = "";
	alert("开始语音识别:");
	plus.speech.startRecognize(options, function(s){
		text += s;
	}, function(e){
		alert("语音识别失败:"+e.message);
	});
}
	</script>
	</head>
	<body>
		<button onclick="startRecognize()">开始识别</button><br/>
		<button onclick="alert(text);">识别内容</button>
	</body>
</html>
				

uni-app使用plus注意事项

stopRecognize

停止语音识别


void plus.speech.stopRecognize();
				

说明:

当语音识别完成时或用户取消语音识别时调用,调用此方法将导致errorCallback回调函数的调用。

参数:

返回值:

void : 无

示例:


<!DOCTYPE html>
<html>
	<head>
	<meta charset="utf-8"/>
	<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
	<title>Speech Example</title>
	<script type="text/javascript">
// 监听plusready事件  
document.addEventListener("plusready", function(){
	// 扩展API加载完毕,现在可以正常调用扩展API
	// ...
}, false );
var text=null;
function startRecognize () {
	var options = {};
	options.engine = 'iFly';
	text = "";
	alert( "开始语音识别:" );
	plus.speech.startRecognize( options, function ( s ) {
		text += s;
	}, function ( e ) {
		alert( "语音识别失败:"+e.message );
	} );
	setTimeout( stopRecognize, 10000 );
}
function stopRecognize(){
	plus.speech.stopRecognize();
}
	</script>
	</head>
	<body>
		<button onclick="startRecognize">开始识别(10s后自动关闭)</button><br/>
		<button onclick="alert(text);">识别内容</button>
	</body>
</html>
				

uni-app使用plus注意事项

addEventListener

监听语音识别事件


void plus.speech.addEventListener(event, listener, capture);
				

说明:

向语音识别模块添加事件监听器,当指定的事件发生时,将触发listener函数的执行。 可多次调用此方法添加多个监听器,当监听的事件发生时,将按照添加的先后顺序触发执行。

参数:

返回值:

void : 无

示例:


<!DOCTYPE html>
<html>
	<head>
	<meta charset="utf-8"/>
	<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
	<title>Speech Example</title>
	<script type="text/javascript">
// 监听plusready事件  
document.addEventListener("plusready", function(){
	// 监听语音识别事件
	plus.speech.addEventListener("start", function(){
		text = null;
	}, false);
	plus.speech.addEventListener("recognition", function(e){
		text += e.result;
	}, false);
	plus.speech.addEventListener("end", function(){
		alert("Success: "+text);
	}, false);
}, false );
var text=null;
function startRecognize() {
	var options = {};
	text = "";
	plus.speech.startRecognize(options);
}
	</script>
	</head>
	<body>
		<button onclick="startRecognize()">开始识别</button><br/>
	</body>
</html>
				

uni-app使用plus注意事项

SpeechRecognizeOptions

JSON对象,语音识别参数


interface plus.speech.SpeechRecognizeOptions {
	attribute Boolean continue;
	attribute String engine;
	attribute String lang;
	attribute Number nbest;
	attribute String punctuation;
	attribute Number timeout;
	attribute Boolean userInterface;
	attribute EventHandler onstart;
	attribute EventHandler onend;
}
				

说明:

控制语音识别引擎内部参数,在JS中为JSON对象,在启动语音识别时使用。

属性:

SpeechRecoginzeEvents

语音识别事件类型

说明:

描述语音过程的触发事件列表,可以通过调用plus.speech.addEventListener方法进行注册监听。

常量:

RecognitionEventCallback

语音识别事件回调函数


void onEvent(event){
	// Event code
}
				

说明:

调用plus.speech.addEventListener方法监听语音识别事件的回调函数。

参数:

返回值:

void : 无

RecognitionSuccessCallback

语音识别成功回调


void onSuccess(result){
	// Recognition success code
}
				

说明:

语音识别成功时的回调函数,用于返回语音识别出的文本内容。

参数:

返回值:

void : 无

RecognitionErrorCallback

语音识别失败回调


void onError(error){
	// Recognition error code
}
				

说明:

当语音识别失败时的回调函数,用于返回语音识别失败的错误信息。

参数:

返回值:

void : 无