码迷,mamicode.com
首页 > 移动开发 > 详细

基于android的语音识别

时间:2017-06-18 10:41:50      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:com   iter   选择   image   tor   set   基于   bsp   源文件   

1.注册账户,添加应用

技术分享

 

2.针对android平台的选择应用,下载SDK

技术分享

 

3.将SDK的libs下文件拷贝到工程的libs目录下

技术分享

4.添加用户权限

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
    <uses-permission android:name="android.permission.CAMERA"/>

5.初始化语音配置对象

        //初始化语音配置对象,必须,这样才能使用MSC语音服务
        SpeechUtility.createUtility(this, SpeechConstant.APPID+"=59454d78");

6.拷贝assets目录下资源文件到android工程的assets目录

技术分享

 

7.识别代码

       //创建语音识别对话框
        RecognizerDialog rd = new RecognizerDialog(this,null);
        //设置参数accent,language等参数
        rd.setParameter(SpeechConstant.LANGUAGE,"zh_cn");//中文
        rd.setParameter(SpeechConstant.ACCENT,"mandarin");//普通话
        //设置回调接口
        rd.setListener(new RecognizerDialogListener() {
            @Override
            public void onResult(RecognizerResult recognizerResult, boolean b) {
                //获取返回结果
                String  result = recognizerResult.getResultString();
                Log.e("result",result);
                Log.e("b",b+"");
            }

            @Override
            public void onError(SpeechError speechError) {

            }
        });
        //显示对话框
        rd.show();

8.由于返回的是json字符串所以还得解析出来才能使用

result:{"sn":2,"ls":true,"bg":0,"ed":0,"ws":[{"bg":0,"cw":[{"sc":0.00,"w":"?"}]}]}

9.解析Json字符串

        /**
         * 解析json串
         * @param result
         */
        protected String parseJsonData(String json) {
        // 使用Gson来解析Json串
            Gson gson = new Gson();
            VoiceBean bean = gson.fromJson(json, VoiceBean.class);
            ArrayList<WS> ws = bean.ws;
            StringBuffer sb = new StringBuffer();
            for (WS ws2 : ws) {
                String info = ws2.cw.get(0).w;
                sb.append(info);
            }
            return sb.toString();
    }


10.完整代码

package com.example.chat;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.example.chat.VoiceBean.WS;
import com.google.gson.Gson;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.ui.RecognizerDialog;
import com.iflytek.cloud.ui.RecognizerDialogListener;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

public class MainActivity extends Activity {
    
    StringBuffer strbuf;
    ListView mlv;
    private List<TalkBean> lists;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化语音配置对象,必须,这样才能使用MSC语音服务
        SpeechUtility.createUtility(this, SpeechConstant.APPID+"=59454d78");
        mlv = (ListView) findViewById(R.id.lv);
        lists = new ArrayList<TalkBean>();
        
    }
    
       public void click(View view){
            //创建语音识别对话框
            RecognizerDialog rd = new RecognizerDialog(this,null);
            //设置参数accent,language等参数
            rd.setParameter(SpeechConstant.LANGUAGE,"zh_cn");//中文
            rd.setParameter(SpeechConstant.ACCENT,"mandarin");//普通话
            strbuf = new StringBuffer();
            //设置回调接口
            rd.setListener(new RecognizerDialogListener() {
                
                @Override
                public void onResult(RecognizerResult arg0, boolean arg1) {
                    // TODO Auto-generated method stub
                    String  result = arg0.getResultString();
                    String data = parseJsonData(result);
                    Log.e("data",data);
                  
                    strbuf.append(data);
                    if(arg1){//回话结束
                        String voice = strbuf.toString();
                        Log.e("result",voice);
                    }
                    
                    Log.e("b",arg1+"");
                }
                
                @Override
                public void onError(SpeechError arg0) {
                    // TODO Auto-generated method stub
                    
                }
            });
            //显示对话框
            rd.show();
        }
        /**
         * 解析json串
         * @param result
         */
        protected String parseJsonData(String json) {
        // 使用Gson来解析Json串
            Gson gson = new Gson();
            VoiceBean bean = gson.fromJson(json, VoiceBean.class);
            ArrayList<WS> ws = bean.ws;
            StringBuffer sb = new StringBuffer();
            for (WS ws2 : ws) {
                String info = ws2.cw.get(0).w;
                sb.append(info);
            }
            return sb.toString();
    }

 

 

11.运行结果展示

技术分享

 

基于android的语音识别

标签:com   iter   选择   image   tor   set   基于   bsp   源文件   

原文地址:http://www.cnblogs.com/huipengbo/p/7043384.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!