设为首页 加入收藏

TOP

安卓Android科大讯飞语音识别代码使用详解(一)
2017-10-11 15:28:07 】 浏览:8579
Tags:安卓 Android 科大 语音 识别 代码 使用 详解

科大讯飞的语音识别功能用在安卓代码中,我把语音识别写成了Service,然后在Fragment直接调用service服务。科大讯飞语音识别用的是带对话框的那个,直接调用科大讯飞的语音接口,代码采用链表结果集的方式获取数据。 
这个语音识别需要在官网申请APPID

本博来自:http://blog.csdn.net/zhaocundang 小波LinuxQQ463431476

测试:

这里写图片描述

这里写图片描述

自己项目采用了科大讯飞语音识别服务,报告中是这样解释的:

语音Service服务代码设计

(1)要想写好Service代码,必须了解Service的生命周期.

(2)首先启动Service服务的方法是: 
getActivity().startService(new Intent(getActivity(),VoiceService.class)); 
停止Service服务: 
getActivity().stopService(new Intent(getActivity(),VoiceService.class)); 
(3)将类继承与Service: 
public class VoiceService extends Service{ 
} 
自动重载OnBind()函数,通过OnBind()的返回值,将Service的实例返回调用者。 
(3) 调用科大讯飞语音API接口代码 
先调用手机麦克风录音: 
rd.setSampleRate(RATE.rate16k); 
调用语音API包中的语音识别对话框,将录音发送到服务器,并接受服务器返回的结果,将数据以链表数据结构的形式传过来,获取结果: 
final StringBuilder sb = new StringBuilder(); 
rd.setListener(new RecognizerDialogListener() { 
public void onResults(ArrayList result, boolean isLast) { 
for (RecognizerResult recognizerResult : result) { 
sb.append(recognizerResult.text); 
} 
} 
public void onEnd(SpeechError error) { 
} 
}); 
(4)文本语音朗读的调用: 
先是声明播放对象: 
private static SynthesizerPlayer player ; 
这里我直接封装一个朗读函数,appid是申请的应用授权id,代码如下: 
public void speak(String words){ 
player = SynthesizerPlayer.createSynthesizerPlayer(getActivity(),”appid=57527406”); 
player.playText(words, null,null); //播放文本 
}

主要的代码:

开启和关闭服务:


public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.button1: getActivity().startService(new Intent(getActivity(),VoiceService.class)); break; case R.id.button2: getActivity().stopService(new Intent(getActivity(),VoiceService.class)); break; } } 

服务中:

package zcd.voice; import java.util.ArrayList; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.view.WindowManager; import android.widget.Toast; import com.iflytek.speech.RecognizerResult; import com.iflytek.speech.SpeechConfig.RATE; import com.iflytek.speech.SpeechError; import com.iflytek.ui.RecognizerDialog; import com.iflytek.ui.RecognizerDialogListener; public class VoiceService extends Service{ private RecognizerDialog rd; private String text; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); // Toast.makeText(this, "Service onCreated", Toast.LENGTH_LONG).show(); rd = new RecognizerDialog(this ,"appid=57627d9c"); } public void onStart(Intent intent, int startId) { // Toast.makeText(this, " Service onStart", Toast.LENGTH_LONG).show(); showReconigizerDialog(); } private void showReconigizerDialog() { //sms 简单语音识别文本 rd.setEngine("sms", null, null); //设置麦克风采样频率 rd.setSampleRate(RATE.rate16k); final StringBuilder re = new StringBuilder(); //设置识别后的回调结果 rd.setListener(new RecognizerDialogListener() { @Override public void onResults(ArrayList<RecognizerResult> result, boolean isLast) { for (RecognizerResult recognizerResult : result) { re.append(recognizerResult.text); } } @Override public void onEnd(SpeechError error) { //识别完成 //R.
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Ionic-wechat项目边开发边学(四.. 下一篇Android PopupWindow怎么合理控制..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目