基于android的语音识别

Posted lovejobs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于android的语音识别相关的知识,希望对你有一定的参考价值。

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.运行结果展示

 源码下载地址:https://download.csdn.net/download/xipengbozai/16500560

以上是关于基于android的语音识别的主要内容,如果未能解决你的问题,请参考以下文章

android如何自定义语音识别

基于神经网络的多音区语音唤醒 | 论文解读

语音识别基于matlab GUI拨号语音识别含Matlab源码 1753期

语音识别基于matlab电话按键语音识别(含按键录音)含Matlab源码 1752期

javaAPI和android sdk接入百度语音识别

语音识别基于LMS算法消除嘈杂的鸟类语音信号中的噪声后识别其对应的鸟类物种(Matlab代码实现)