科大讯飞语音服务-只含听写

来源:互联网 发布:淘宝店铺一键复制软件 编辑:程序博客网 时间:2024/05/01 09:11

科大讯飞语音服务-只含听写

想要集成科大讯飞语音服务首先要在人家的平台上注册一个自己的开发者账号 
科大讯飞语音-讯飞开放平台地址:http://www.xfyun.cn/?ch=bdtg

进入地址之后要登录自己的账号,如果没有的话就自己注册一个账号 
登陆

登陆之后点击控制台进入到我的语音云页面,在里面找到创建新应用

创建新应用

创建完成之后就开通自己所需要的一些服务

这里写图片描述

然后就是下载人家的SDK了,下载完成之后就将里面的libs包和assets分别导入到自己的项目里面,上面的导航栏上有SDK下载,点击进入选择自己想要完成的效果(目前这里只有听写服务)然后选择平台基于自己的开发平台选择,最后选择自己的应用,就是自己刚创建的新应用,点击下载SDK,我们所需要的东西都在下载的那个.zip文件里面了,先将libs里面的两个jar包导入到自己的工程里面,还有一个sample文件,里面有三个项目,可以导入到工程中运行看看效果

libs里面加入两个jar包

导jar包 
然后将剩下的文件添加到jniLibs里面,这个jniLibs放在自己的main里面 
这里写图片描述

然后将assets整个文件拷到自己项目的main里面

导assets

然后就开始我们的代码………..

1、activity-main布局文件当中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="text.bwei.com.kedaxunfei2.MainActivity">    <TextView        android:id="@+id/textView"        android:layout_width="match_parent"        android:layout_height="400dp"        android:background="#c7edcc"        android:text="@string/hello_world" />    <Button        android:id="@+id/btn"        android:layout_width="match_parent"        android:layout_height="50dp"        android:text="开始" /></LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

2、新建一个类JsonParser

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();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90

3、新建一个MyApplication类继承Application 
里面装的是自己在科大讯飞语音开放平台里申请到的APPID值

APPID

import android.app.Application;import com.iflytek.cloud.SpeechConstant;import com.iflytek.cloud.SpeechUtility;public class MyApplication extends Application {    @Override    public void onCreate() {        SpeechUtility.createUtility(getApplicationContext(), SpeechConstant.APPID+"=5832dda6");        super.onCreate();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

4、在MainActivity里面

import android.content.Context;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;import com.iflytek.cloud.ErrorCode;import com.iflytek.cloud.InitListener;import com.iflytek.cloud.RecognizerResult;import com.iflytek.cloud.SpeechConstant;import com.iflytek.cloud.SpeechError;import com.iflytek.cloud.ui.RecognizerDialog;import com.iflytek.cloud.ui.RecognizerDialogListener;public class MainActivity extends AppCompatActivity {    public String tag = "MainActivity";    public String text;    public TextView textView;    private Button btn;    public Context context = MainActivity.this;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        textView = (TextView) findViewById(R.id.textView);        btn = (Button) findViewById(R.id.btn);        btn.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                initRecognizerDialog();            }        });    }    private void initRecognizerDialog() {        // 1.创建RecognizerDialog对象        RecognizerDialog mDialog = new RecognizerDialog(context, initListener);        // 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(mRecognizerDialogListener);        // 4.显示dialog,接收语音输入        mDialog.show();        text = "";    }    InitListener initListener = new InitListener() {        @Override        public void onInit(int code) {            if (code != ErrorCode.SUCCESS) {                Toast.makeText(context, "监听器初始化错误,错误代码=" + code,                        Toast.LENGTH_SHORT).show();            }        }    };    RecognizerDialogListener mRecognizerDialogListener = new RecognizerDialogListener() {        @Override        public void onResult(RecognizerResult result, boolean isLast) {            String json = result.getResultString();            String content = JsonParser.parseIatResult(json);            text += content;            Log.d(tag, "###content=" + content);            textView.setText(text);        }        @Override        public void onError(SpeechError error) {            Toast.makeText(context, error.getErrorDescription(),                    Toast.LENGTH_SHORT).show();        }    };}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85

最后是 manifest清单文件里面添加自己的需要的权限,别忘了在application里面添加自己的App的name

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="text.bwei.com.kedaxunfei2">    <!-- 科大讯飞   start -->    <!-- 连接网络权限,用于执行云端语音能力 -->    <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" />    <!-- 下面3个权限有空格导致报错,记得去掉空格 -->    <!-- 外存储写权限,构建语法需要用到此权限 -->    <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" />    <!-- 科大讯飞   end -->    <application        android:name=".MyApplication"        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

然后、、、、然后、、、、然后就没了,你已经完成了,赶快运行一下看看自己做的效果吧

效果图 
刚开始 
按下开始听写 
这里写图片描述 

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 网吧吃鸡更新慢怎么办 手机号注册不了微信怎么办 小米账号密码忘了怎么办 小米手机账号密码忘了怎么办 华为手机账号密码忘记了怎么办 老年机开不了机怎么办 天谕没有顺网登陆怎么办 苹果密保问题忘了怎么办 密保手机没用了怎么办 qq密保手机没用了怎么办 手机开机按钮坏了怎么办 改了账号游戏角色消失怎么办 华为开机键坏了怎么办 抖音账号已重置怎么办 抖音账号被重置怎么办 吃鸡账号密码忘了怎么办 微信只记得账号忘了手机号怎么办 红米3开机键失灵怎么办 晚自习教室有许多虫子怎么办 泰迪吃草又呕吐怎么办 手机不断收到验证码信息怎么办 樱桃吃多了上火怎么办 过年不想回婆婆家过怎么办 旅行箱提手坏了怎么办 影棚人物后面有影子怎么办 微信运动图标不见了怎么办 逆光拍摄人黑了怎么办 单反镜头刮花了怎么办 股东各50股份不同意退股怎么办 退股没有协议他不愿意退钱怎么办 s7刷机有三星帐号id怎么办 做主播留不住人怎么办 直播间留不住人怎么办 淘宝直播间留不住人怎么办 干了一个月不发工资怎么办 16岁长白色头发怎么办 腾讯乘车码解约了怎么办 蓝洞棋牌客封号怎么办 草莓被蚂蚁吃了怎么办 脖子被种了草莓怎么办 2岁的宝宝说脏话怎么办