Android集成讯飞SDK实现语音拨号、语音导航、语音启动应用

转载请注明出处:周木水的CSDN博客

科大讯飞语音SDK的语义分析还是挺强大的,,可使我们的应用更加强大。 上篇博文介绍了讯飞SDK的一些简单功能: Android 使用讯飞语音SDK

今天来看看对语义分析结果JSON的解析并处理:

实现语音拨号

首先,我们看看“打电话给张三”这句话在服务器分析之后,传给我们的JSON是什么样的:

{“semantic”: {“slots”: {“name”: “张三”}},”rc”: 0,”operation”: “CALL”,”service”: “telephone”,”text”: “打电话给张三。”}

所以,我们的思路就是获取到name,然后和联系人ContentProvider中的联系人DisplayName进行逐一匹配。但是要考虑到同音字的问题(例如:“打电话给张晓静”,服务器返回的name是”张小静“,这就没法匹配。)在没有匹配的情况下,我们将name转成拼音,然后再和联系人拼音进行对比即可。

看一下核心代码:

if (“telephone”.equals(strService)) {// “operation”: “CALL”String peopleName = jsonObject.getJSONObject(“semantic”).getJSONObject(“slots”).getString(“name”);String operationStr = jsonObject.getString(“operation”);if (“CALL”.equals(operationStr)) {String phoneNum = getContactNumberByName(peopleName);String phoneCode = “”;try {phoneCode = jsonObject.getJSONObject(“semantic”).getJSONObject(“slots”).getString(“code”);} catch (Exception e) {}if (phoneNum != null & phoneNum.trim().length() > 0) {String strAnswer = “正在打电话给:” + peopleName;tvAnswer.setText(strAnswer);startSpeak(strAnswer);Uri uri = Uri.parse(“tel:” + phoneNum);Intent intent = new Intent(Intent.ACTION_CALL, uri);startActivity(intent);} else if (phoneCode != null& phoneCode.trim().length() > 0) {String strAnswer = “正在打电话给:” + peopleName;tvAnswer.setText(strAnswer);startSpeak(strAnswer);Uri uri = Uri.parse(“tel:” + phoneCode);Intent intent = new Intent(Intent.ACTION_CALL, uri);startActivity(intent);} else {String phoneNumFromPinYin = getContactNumberByPinYin(PinYinUtil.convertAll(peopleName));if (phoneNumFromPinYin != null& phoneNumFromPinYin.trim().length() > 0) {String strAnswer = “正在打电话给:” + peopleName;tvAnswer.setText(strAnswer);startSpeak(strAnswer);Uri uri = Uri.parse(“tel:” + phoneNumFromPinYin);Intent intent = new Intent(Intent.ACTION_CALL, uri);startActivity(intent);} else {String strAnswer = “通讯录中未找到:” + peopleName;tvAnswer.setText(strAnswer);startSpeak(strAnswer);}}}}

语音导航

这里不考虑”从A到B怎么走(A,B都为异地)“的情况。起点不是当前位置的情况解析方式相同,只是不常用。所以这里的例子起点默认为当前定位位置。 当我们说”导航到深圳北站“时,服务器返回的JSON如下:

{“semantic”: {“slots”: {“endLoc”: {“type”: “LOC_POI”,”poi”: “深圳南站”,”city”: “深圳市”,”cityAddr”: “深圳”},”startLoc”: {“type”: “LOC_POI”,”city”: “CURRENT_CITY”,”poi”: “CURRENT_POI”}}},”rc”: 0,”operation”: “ROUTE”,”service”: “map”,”text”: “导航到深圳南站。”}

首先我们对JSON进行解析:

(strService)) {// operation”: “ROUTE”String endPoiStr = jsonObject.getJSONObject(“semantic”).getJSONObject(“slots”).getJSONObject(“endLoc”).getString(“poi”);String endCityStr = jsonObject.getJSONObject(“semantic”).getJSONObject(“slots”).getJSONObject(“endLoc”).getString(“city”);String endAddressStr = “”;(endCityStr))endCityStr = mSharedPreferences.getString(“cityName”, “未知”);}只要笑一笑,没什么事请过不了

Android集成讯飞SDK实现语音拨号、语音导航、语音启动应用

相关文章:

你感兴趣的文章:

标签云: