android 语音集成

讯飞语音开发集成地址http://www.xfyun.cn/ 解压后的doc文件夹下的msc develop 文件中有详细集成步骤 
这里写图片描述 
AppId:

这里写图片描述 
1.先要注册开发者账户, 添加我的应用 , 下载sdk

这里写图片描述

2.下载后将sdk解压, 把案例导入工程中运行看看效果 
这里写图片描述

3.将libs下的两个jar包添加到libs目录下, 将同路径下的其它 .so文件(与c进行交互)复制到main路径下新建的 jniLibs(L要大写)目录下(别忘了jar包要add) , 将assert目录拷贝到main目录下 
注意 : 这些都是自己创建的应用生成的, 在申请appid时就与自己的应用绑定了, 拷贝别人的是没有用的

这里写图片描述

4.添加权限

5.创建类 , 以下是代码: initSpeech()方法下的APPID需要改成上面介绍中自己应用生成的appId

 package atguigu.com.speechdemo2;

import android.app.Activity ;
import android.os.Bundle ;
import android.util.Log ;
import android.view.View ;
import android.widget.Button ;
import android.widget.EditText ;
import android.widget.Toast ;

import com.iflytek.cloud.ErrorCode ;
import com.iflytek.cloud.InitListener ;
import com.iflytek.cloud.RecognizerListener ;
import com.iflytek.cloud.RecognizerResult ;
import com.iflytek.cloud.SpeechConstant ;
import com.iflytek.cloud.SpeechError ;
import com.iflytek.cloud.SpeechRecognizer ;
import com.iflytek.cloud.SpeechSynthesizer ;
import com.iflytek.cloud.SpeechUtility ;
import com.iflytek.cloud.SynthesizerListener ;
import com.iflytek.cloud.ui.RecognizerDialog ;
import com.iflytek.cloud.ui.RecognizerDialogListener ;

import org.json.JSONException ;
import org.json.JSONObject ;

import java.util.HashMap ;
import java.util.LinkedHashMap ;

public class MainActivity extends Activity implements View.OnClickListener {

    private static final String TAG = MainActivity.class .getSimpleName();
    private EditText et_input;
    private Button btn_startspeech, btn_startspeektext ;

    // 用HashMap存储听写结果
    private HashMap<String, String> mIatResults = new LinkedHashMap<String , String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super .onCreate(savedInstanceState) ;
        initView() ;
        initSpeech() ;
    }

    private void initView() {
        setContentView(R.layout.activity_main) ;
        et_input = (EditText) findViewById(R.id.et_input );
        btn_startspeech = (Button) findViewById(R.id.btn_startspeech );
        btn_startspeektext = (Button) findViewById(R.id.btn_startspeektext );
        btn_startspeech .setOnClickListener(this) ;
        btn_startspeektext .setOnClickListener(this) ;
    }

    private void initSpeech() {
        // 将“12345678”替换成您申请的 APPID,申请地址: http://www.xfyun.cn
        // 请勿在 “ =”与 appid 之间添加任务空字符或者转义符
        SpeechUtility. createUtility( this, SpeechConstant. APPID + "=56ffe0ae" ); 
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_startspeech: //语音识别(把声音转文字)
                startSpeechDialog();
                break;
            case R.id. btn_startspeektext:// 语音合成(把文字转声音)
                speekText();
                break;
        }

    }

    private void speekText() {
        //1. 创建 SpeechSynthesizer 对象 , 第二个参数: 本地合成时传 InitListener
        SpeechSynthesizer mTts = SpeechSynthesizer.createSynthesizer( this, null);
//2.合成参数设置,详见《 MSC Reference Manual》 SpeechSynthesizer 类
//设置发音人(更多在线发音人,用户可参见 附录 13.2
        mTts.setParameter(SpeechConstant. VOICE_NAME, "vixyun" ); // 设置发音人
        mTts.setParameter(SpeechConstant. SPEED, "50" );// 设置语速
        mTts.setParameter(SpeechConstant. VOLUME, "80" );// 设置音量,范围 0~100
        mTts.setParameter(SpeechConstant. ENGINE_TYPE, SpeechConstant. TYPE_CLOUD); //设置云端
//设置合成音频保存位置(可自定义保存位置),保存在 “./sdcard/iflytek.pcm”
//保存在 SD 卡需要在 AndroidManifest.xml 添加写 SD 卡权限
//仅支持保存为 pcm 和 wav 格式, 如果不需要保存合成音频,注释该行代码
        mTts.setParameter(SpeechConstant. TTS_AUDIO_PATH, "./sdcard/iflytek.pcm" );
//3.开始合成
        mTts.startSpeaking( et_input.getText().toString(), new MySynthesizerListener()) ;

    }

    class MySynthesizerListener implements SynthesizerListener {

        @Override
        public void onSpeakBegin() {
            showTip(" 开始播放 ");
        }

        @Override
        public void onSpeakPaused() {
            showTip(" 暂停播放 ");
        }

        @Override
        public void onSpeakResumed() {
            showTip(" 继续播放 ");
        }

        @Override
        public void onBufferProgress(int percent, int beginPos, int endPos ,
                                     String info) {
            // 合成进度
        }

        @Override
        public void onSpeakProgress(int percent, int beginPos, int endPos) {
            // 播放进度
        }

        @Override
        public void onCompleted(SpeechError error) {
            if (error == null) {
                showTip("播放完成 ");
            } else if (error != null ) {
                showTip(error.getPlainDescription( true));
            }
        }

        @Override
        public void onEvent(int eventType, int arg1 , int arg2, Bundle obj) {
            // 以下代码用于获取与云端的会话 id,当业务出错时将会话 id提供给技术支持人员,可用于查询会话日志,定位出错原因
            // 若使用本地能力,会话 id为null
            //if (SpeechEvent.EVENT_SESSION_ID == eventType) {
            //     String sid = obj.getString(SpeechEvent.KEY_EVENT_SESSION_ID);
            //     Log.d(TAG, "session id =" + sid);
            //}
        }
    }

    private void startSpeechDialog() {
        //1. 创建RecognizerDialog对象
        RecognizerDialog mDialog = new RecognizerDialog(this, new MyInitListener()) ;
        //2. 设置accent、 language等参数
        mDialog.setParameter(SpeechConstant. LANGUAGE, "zh_cn" );// 设置中文
        mDialog.setParameter(SpeechConstant. ACCENT, "mandarin" );
        // 若要将UI控件用于语义理解,必须添加以下参数设置,设置之后 onResult回调返回将是语义理解
        // 结果
        // mDialog.setParameter("asr_sch", "1");
        // mDialog.setParameter("nlp_version", "2.0");
        //3.设置回调接口
        mDialog.setListener( new MyRecognizerDialogListener()) ;
        //4. 显示dialog,接收语音输入
        mDialog.show() ;
    }

    class MyRecognizerDialogListener implements RecognizerDialogListener {

        /**
         * @param results
         * @param isLast  是否说完了
         */
        @Override
        public void onResult(RecognizerResult results, boolean isLast) {
            String result = results.getResultString(); //为解析的
            showTip(result) ;
            System. out.println(" 没有解析的 :" + result);

            String text = JsonParser.parseIatResult(result) ;//解析过后的
            System. out.println(" 解析后的 :" + text);

            String sn = null;
            // 读取json结果中的 sn字段
            try {
                JSONObject resultJson = new JSONObject(results.getResultString()) ;
                sn = resultJson.optString("sn" );
            } catch (JSONException e) {
                e.printStackTrace();
            }

            mIatResults .put(sn, text) ;//没有得到一句,添加到

            StringBuffer resultBuffer = new StringBuffer();
            for (String key : mIatResults.keySet()) {
                resultBuffer.append(mIatResults .get(key));
            }

            et_input.setText(resultBuffer.toString());// 设置输入框的文本
            et_input .setSelection(et_input.length()) ;//把光标定位末尾
        }

        @Override
        public void onError(SpeechError speechError) {

        }
    }

    class MyInitListener implements InitListener {

        @Override
        public void onInit(int code) {
            if (code != ErrorCode.SUCCESS) {
                showTip("初始化失败 ");
            }

        }
    }

    /**
     * 语音识别
     */
    private void startSpeech() {
        //1. 创建SpeechRecognizer对象,第二个参数: 本地识别时传 InitListener
        SpeechRecognizer mIat = SpeechRecognizer.createRecognizer( this, null); //语音识别器
        //2. 设置听写参数,详见《 MSC Reference Manual》 SpeechConstant类
        mIat.setParameter(SpeechConstant. DOMAIN, "iat" );// 短信和日常用语: iat (默认)
        mIat.setParameter(SpeechConstant. LANGUAGE, "zh_cn" );// 设置中文
        mIat.setParameter(SpeechConstant. ACCENT, "mandarin" );// 设置普通话
        //3. 开始听写
        mIat.startListening( mRecoListener);
    }


    // 听写监听器
    private RecognizerListener mRecoListener = new RecognizerListener() {
        // 听写结果回调接口 (返回Json 格式结果,用户可参见附录 13.1);
//一般情况下会通过onResults接口多次返回结果,完整的识别内容是多次结果的累加;
//关于解析Json的代码可参见 Demo中JsonParser 类;
//isLast等于true 时会话结束。
        public void onResult(RecognizerResult results, boolean isLast) {
            Log.e (TAG, results.getResultString());
            System.out.println(results.getResultString()) ;
            showTip(results.getResultString()) ;
        }

        // 会话发生错误回调接口
        public void onError(SpeechError error) {
            showTip(error.getPlainDescription(true)) ;
            // 获取错误码描述
            Log. e(TAG, "error.getPlainDescription(true)==" + error.getPlainDescription(true ));
        }

        // 开始录音
        public void onBeginOfSpeech() {
            showTip(" 开始录音 ");
        }

        //volume 音量值0~30, data音频数据
        public void onVolumeChanged(int volume, byte[] data) {
            showTip(" 声音改变了 ");
        }

        // 结束录音
        public void onEndOfSpeech() {
            showTip(" 结束录音 ");
        }

        // 扩展用接口
        public void onEvent(int eventType, int arg1 , int arg2, Bundle obj) {
        }
    };

    private void showTip (String data) {
        Toast.makeText( this, data, Toast.LENGTH_SHORT).show() ;
    }
}

6.json解析类:

package atguigu.com.speechdemo2;

import org.json.JSONArray ;
import org.json.JSONObject ;
import org.json.JSONTokener ;

/**
* Json结果解析类
*/
public class JsonParser {

     public static String parseIatResult(String json) {
          StringBuffer ret = new StringBuffer() ;
          try {
              JSONTokener tokener = new JSONTokener(json) ;
              JSONObject joResult = new JSONObject(tokener) ;

              JSONArray words = joResult.getJSONArray("ws" );
              for (int i = 0; i < words.length(); i++) {
                   // 转写结果词,默认使用第一个结果
                                       JSONArray items = words.getJSONObject(i).getJSONArray("cw" );
                   JSONObject obj = items.getJSONObject(0 );
                   ret.append(obj.getString("w" ));
//                  如果需要多候选结果,解析数组其他字段
//                 for(int j = 0; j < items.length(); j++)
//                 {
//                      JSONObject obj = items.getJSONObject(j);
//                      ret.append(obj.getString("w"));
//                 }
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
          return ret.toString();
     }

     public static String parseGrammarResult(String json) {
          StringBuffer ret = new StringBuffer() ;
          try {
              JSONTokener tokener = new JSONTokener(json) ;
              JSONObject joResult = new JSONObject(tokener) ;

              JSONArray words = joResult.getJSONArray("ws" );
              for (int i = 0; i < words.length(); i++) {
                   JSONArray items = words.getJSONObject(i).getJSONArray("cw" );
                   for (int j = 0; j < items.length() ; j++)
                   {
                        JSONObject obj = items.getJSONObject(j);
                        if (obj.getString("w").contains( "nomatch"))
                        {
                             ret.append( "没有匹配结果.") ;
                             return ret.toString();
                         }
                        ret.append( "【结果】" + obj.getString("w" ));
                         ret.append("【置信度】 " + obj.getInt("sc" ));
                         ret.append("\n ");
                   }
              }
          } catch (Exception e) {
              e.printStackTrace();
              ret.append(" 没有匹配结果 .");
          }
          return ret.toString();
     }

     public static String parseLocalGrammarResult(String json) {
          StringBuffer ret = new StringBuffer() ;
          try {
              JSONTokener tokener = new JSONTokener(json) ;
              JSONObject joResult = new JSONObject(tokener) ;

              JSONArray words = joResult.getJSONArray("ws" );
              for (int i = 0; i < words.length(); i++) {
                   JSONArray items = words.getJSONObject(i).getJSONArray("cw" );
                   for (int j = 0; j < items.length() ; j++)
                   {
                        JSONObject obj = items.getJSONObject(j);
                        if (obj.getString("w").contains( "nomatch"))
                        {
                             ret.append( "没有匹配结果.") ;
                             return ret.toString();
                         }
                        ret.append( "【结果】" + obj.getString("w" ));
                         ret.append("\n ");
                   }
              }
              ret.append("【置信度】 " + joResult.optInt("sc" ));

          } catch (Exception e) {
              e.printStackTrace();
              ret.append(" 没有匹配结果 .");
          }
          return ret.toString();
     }
}

7.布局文件: 

<? xml version="1.0" encoding= "utf-8"?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
    android :layout_width="match_parent"
    android :layout_height="match_parent"
    android :orientation="vertical"
    >

    <EditText
        android :id="@+id/et_input"
        android :layout_margin="10dp"
        android :layout_width="match_parent"
        android :layout_height="80dp"
        android :hint="请输入文本信息 ..." />


    <Button
        android :id="@+id/btn_startspeech"
        android :text="点击按钮语音输入 "
        android :layout_width="match_parent"
        android :layout_height="wrap_content" />

    <Button
        android :id="@+id/btn_startspeektext"
        android :text="语音合成(把文字转声音) "
        android :layout_width="match_parent"
        android :layout_height="wrap_content" />


</LinearLayout>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
About Wouldn't your prefer to let your users speak instead of making them type? This plugin uses OS components for speech recognition and send it to your Unity scripts as String objects. Plugin supports: - Android >= 3.0 (haven’t tested below, it might work though… ), - iOS >= 10.0. That doesn’t mean you can’t target iOS lower than 10 - you simply have to prepare fallback code to cover cases when user doesn’t have access to speech recognition (SpeechRecognizer.EngineExists() for the help!). Keep in mind that both iOS and Android might use Internet connection for speech detection, which means it might fail in case there’s no active connection. Plugin doesn’t work in Editor! You have to run your app on real iOS or Android device. MOBILE SPEECH RECOGNIZER - UNITY PLUGIN ?2 Quick Start Open example scene Go to KKSpeechRecognizer/Example folder inside Unity and open ExampleScene: It shows basic usage of a plugin, which is: 1. Detecting if speech recognition exists on user’s device (keep in mind that it won’t be available on e.g. iOS 9 or old Android phones), 2. If it exists, and user clicks on “Start Recording” button it listens for recognized text and displays it on a screen, 3. On Android, speech recognition automatically detects when user finishes speaking, but on iOS we have to wait for user clicking “Stop Recording” to finish whole process (i.e. get final results). Before running it on Android or iOS device you have to… Setup permissions iOS After generating Xcode project (keep in mind that you have to use Xcode 8 or higher) you have to add two permissions keys to your project: MOBILE SPEECH RECOGNIZER - UNITY PLUGIN ?3 NSMicrophoneUsageDescription explanation from Apple docs: This key lets you describe the reason your app accesses any of the the device’s microphones. When the system prompts the user to allow access, this string is displayed as part of the alert. NSSpeechRecognitionUsageDescription explanation from Apple docs: This key lets you describe the reason y
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值