android128 zhihuibeijing 科大讯飞 语音识别

  1. 云栖社区>
  2. 博客>
  3. 正文

android128 zhihuibeijing 科大讯飞 语音识别

范大脚脚 2017-11-14 03:08:00 浏览1185
展开阅读全文
复制代码
package com.itheima.voicedemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

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;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化语音引擎
        SpeechUtility.createUtility(this, SpeechConstant.APPID + "=54b8bca3");
    }

    /**
     * 开始听写
     */
    public void listen(View view) {
        // 1.创建SpeechRecognizer对象,第二个参数:本地听写时传InitListener
        SpeechRecognizer mIat = SpeechRecognizer.createRecognizer(this, null);

        // 2.设置听写参数,详见《科大讯飞MSC API手册(Android)》SpeechConstant类
        mIat.setParameter(SpeechConstant.DOMAIN, "iat");
        mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
        mIat.setParameter(SpeechConstant.ACCENT, "mandarin");

        // 开始听写
        mIat.startListening(mRecoListener);

    }

    /**
     * 交互动画
     */
    public void listenUI(View view) {
        RecognizerDialog iatDialog = new RecognizerDialog(this, mInitListener);

        // 2.设置听写参数,详见《科大讯飞MSC API手册(Android)》SpeechConstant类
        iatDialog.setParameter(SpeechConstant.DOMAIN, "iat");
        iatDialog.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
        iatDialog.setParameter(SpeechConstant.ACCENT, "mandarin");

        iatDialog.setListener(recognizerDialogListener);

        iatDialog.show();
    }

    /**
     * 语音朗诵
     */
    public void read(View view) {
        SpeechSynthesizer mTts = SpeechSynthesizer
                .createSynthesizer(this, null);

        mTts.setParameter(SpeechConstant.VOICE_NAME, "vixm");
        mTts.setParameter(SpeechConstant.SPEED, "50");
        mTts.setParameter(SpeechConstant.VOLUME, "80");
        mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);

        mTts.startSpeaking("床前明月光,地上鞋两双,床上狗男女,其中就有你! 你好啊?",
                mSynthesizerListener);
    }

    private SynthesizerListener mSynthesizerListener = new SynthesizerListener() {

        @Override
        public void onSpeakResumed() {

        }

        @Override
        public void onSpeakProgress(int arg0, int arg1, int arg2) {

        }

        @Override
        public void onSpeakPaused() {

        }

        @Override
        public void onSpeakBegin() {

        }

        @Override
        public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {

        }

        @Override
        public void onCompleted(SpeechError arg0) {

        }

        @Override
        public void onBufferProgress(int arg0, int arg1, int arg2, String arg3) {

        }
    };

    private RecognizerDialogListener recognizerDialogListener = new RecognizerDialogListener() {

        @Override
        public void onResult(RecognizerResult results, boolean isLast) {
            System.out.println(results.getResultString());
            System.out.println("isLast=" + isLast);
        }

        @Override
        public void onError(SpeechError arg0) {

        }
    };

    private InitListener mInitListener = new InitListener() {

        @Override
        public void onInit(int arg0) {
        }
    };

    private RecognizerListener mRecoListener = new RecognizerListener() {

        /**
         * 语音识别结果 isLast=true表示会话结束
         */
        @Override
        public void onResult(RecognizerResult results, boolean isLast) {
            System.out.println(results.getResultString());//打印听到的结果。
        }

        @Override
        public void onBeginOfSpeech() {

        }

        @Override
        public void onEndOfSpeech() {

        }

        @Override
        public void onError(SpeechError arg0) {

        }

        @Override
        public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {

        }

        @Override
        public void onVolumeChanged(int arg0) {

        }
    };
}
复制代码
复制代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itheima.voicedemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <!-- 连接网络权限,用于执行云端语音能力 -->
    <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" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.itheima.voicedemo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
复制代码

 



本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/5080174.html,如需转载请自行联系原作者

网友评论

登录后评论
0/500
评论
范大脚脚
+ 关注