Android 学习——语音识别

语音识别

科大讯飞开放平台

官网:http://www.xfyun.cn/

开发文档:https://doc.xfyun.cn/msc_android/index.html

创建应用

下载SDK,选择需要的功能

导入SDK

添加权限

<!--连接网络权限,用于执行云端语音能力 -->
<uses-permission android:name="android.permission.INTERNET"/>
<!--获取手机录音机使用权限,听写、识别、语义理解需要用到此权限 -->
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<!--读取网络信息状态 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!--获取当前wifi状态 -->
<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"/>
<!--手机定位信息,用来为语义等功能提供定位,提供更精准的服务-->
<!--定位信息是敏感信息,可通过Setting.setLocationEnable(false)关闭定位请求 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!--如需使用人脸识别,还要添加:摄相头权限,拍照需要用到 -->
<uses-permission android:name="android.permission.CAMERA" />

初始化

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

语音听写

package com.example.yuyin;

import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

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.SpeechUtility;

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

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


public class MainActivity extends AppCompatActivity {

    private TextView tv_content;
    private HashMap<String,String> nIatResults=new LinkedHashMap<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 将“12345678”替换成您申请的APPID,申请地址:http://www.xfyun.cn
        // 请勿在“=”与appid之间添加任何空字符或者转义符
        SpeechUtility.createUtility(this, SpeechConstant.APPID +"=5ce4a75c");
        tv_content=findViewById(R.id.info);
    }

    //语音听写
    public void startSpeachClick(View view){
        //初始化识别无UI识别对象
        //使用SpeechRecognizer对象,可根据回调消息自定义界面;
        SpeechRecognizer mIat = SpeechRecognizer.createRecognizer(this, null);

        //设置语法ID和 SUBJECT 为空,以免因之前有语法调用而设置了此参数;或直接清空所有参数,具体可参考 DEMO 的示例。
        mIat.setParameter( SpeechConstant.DOMAIN, "iat" );//应用领域
        //设置语音输入语言,zh_cn为简体中文
        mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
        //设置结果返回语言,普通话
        mIat.setParameter(SpeechConstant.ACCENT, "mandarin");
        mIat.setParameter(SpeechConstant.ENGINE_TYPE,SpeechConstant.TYPE_CLOUD);
        //设置返回结果格式
        mIat.setParameter(SpeechConstant.RESULT_TYPE,"json");
        //设置语音前端点:静音超时时间,单位ms,即用户多长时间不说话则当做超时处理
        //取值范围{1000~10000}
        mIat.setParameter(SpeechConstant.VAD_BOS, "4000");
        //设置语音后端点:后端点静音检测时间,单位ms,即用户停止说话多长时间内即认为不再输入,
        //自动停止录音,范围{0~10000}
        mIat.setParameter(SpeechConstant.VAD_EOS, "1000");
        //设置标点符号,设置为"0"返回结果无标点,设置为"1"返回结果有标点
        mIat.setParameter(SpeechConstant.ASR_PTT,"1");
        //设置听写的动态修正
        mIat.setParameter(SpeechConstant.ASR_DWA,"0");

        //开始识别,并设置监听器
        mIat.startListening(mRecogListener);
    }

    private RecognizerListener mRecogListener = new RecognizerListener() {
        //0-30音量调节
        @Override
        public void onVolumeChanged(int i, byte[] bytes) {

        }
        //开始录音
        @Override
        public void onBeginOfSpeech() {

        }
        //停止录音
        @Override
        public void onEndOfSpeech(){

        }

        @Override
        public void onError(SpeechError speechError) {

        }

        //返回结果
        @Override
        public void onResult(RecognizerResult recognizerResult, boolean b) {
            Log.i("tag",recognizerResult.getResultString());
            printResult(recognizerResult);
        }

        @Override
        public void onEvent(int i, int i1, int i2, Bundle bundle) {

        }
    };

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

        nIatResults.put(sn,text);

        StringBuffer resultBuffer=new StringBuffer();
        for (String key:nIatResults.keySet()){
            resultBuffer.append(nIatResults.get(key));
        }
        tv_content.setText(resultBuffer.toString());
    }
}

语音合成

package com.example.yuyin;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.SynthesizerListener;

public class Main2Activity extends AppCompatActivity {

    private EditText editText_content;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        SpeechUtility.createUtility(this, SpeechConstant.APPID +"=5ce4a75c");
        editText_content=findViewById(R.id.wenzi);
    }

    public void startClick(View view){
        //创建SpeechSynthesizer
        SpeechSynthesizer mTts=SpeechSynthesizer.createSynthesizer(this,null);
        //合成参数
        mTts.setParameter(SpeechConstant.VOICE_NAME,"xiaoyan");
        mTts.setParameter(SpeechConstant.SPEED,"50");//语速
        mTts.setParameter( SpeechConstant.VOLUME, "80");//音量
        mTts.setParameter( SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD );
        //保存路径
//        mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH,"./sdcard/iflutek.pcm");
        //合成
        String content=editText_content.getText().toString();
        mTts.startSpeaking(content,mSynListener);
    }

    //合成监听器
    private SynthesizerListener mSynListener=new SynthesizerListener() {
        @Override
        public void onSpeakBegin() {

        }

        @Override
        public void onBufferProgress(int i, int i1, int i2, String s) {

        }

        @Override
        public void onSpeakPaused() {

        }

        @Override
        public void onSpeakResumed() {

        }

        @Override
        public void onSpeakProgress(int i, int i1, int i2) {

        }

        @Override
        public void onCompleted(SpeechError speechError) {

        }

        @Override
        public void onEvent(int i, int i1, int i2, Bundle bundle) {

        }
    };
}

 

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、付费专栏及课程。

余额充值