Teach you to do the secondary development of Android, the recognition rate IFLYTEK voice input level | Force program

Author | Pek_KuaiJia

Zebian | yugao

Head Figure | CSDN downloaded from the Vision China

Exhibition | CSDN (ID: CSDNnews)

With the current fine and intelligent user needs, we need a lot of time in the App integrated voice input module, to provide users with voice input functionality. The IFLYTEK voice as the industry leader, relatively accurate recognition results, and comes with a set of identification information flying animation for fast building blocks, ado, look at renderings.


The following specific steps begin

Application key

Baidu search news fly open platform, and registered account real-name authentication, and then locate the voice dictation product, you can receive a 90-day trial package, the commercial fee. Then open the console, you need to use the application to add information to fly the SDK, pay attention to save APPID here, then this is the need to configure the APP.



Download SDK

Found in the official website database, select the application you just added, download online speech recognition SDK


Under the pressure resulting libs folder Msc.jar file under libs copied to the project

If you do not create a new one, after the copy is complete, right-click Msc.jar file, select add as library, get behind the speech recognition result here need to parse json, because I use the gson, so be introduced; then downloaded libs folder two folders to your project directory src / main / jniLibs, if not the folder create a new one; new assets last file in the app directory under your project folder, the download folder in assets copy the folder into it, so far, all the file import is complete, put the whole project file structure.


Add permissions for voice dictation

Add mainfest.xml can pay attention to more than Android6.0, microphone and access to mobile phones to read the identification code permission requires dynamic applications.

 1  <uses-permission android:name="android.permission.INTERNET" />
 2    <!--获取手机录音机使用权限,听写、识别、语义理解需要用到此权限 -->
 3    <uses-permission android:name="android.permission.RECORD_AUDIO" />
 4    <!--读取网络信息状态 -->
 5    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 6    <!--获取当前wifi状态 -->
 7    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
 8    <!--允许程序改变网络连接状态 -->
 9    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
10    <!--读取手机信息权限 -->
11    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

Configuration APPID

Application of the following code arranged onCreate APP's (), if no file application, may be a need to use this feature onCreate Activity () Add note "=" not less

 1public class MyApplication extends Application{
 2
 3    @Override
 4    public void onCreate() {
 5        super.onCreate();
 6        //注意这里的“=”不能少
 7        SpeechUtility.createUtility(getApplicationContext();, SpeechConstant.APPID + "=你的APPID");
 8
 9    }
10
11}

Add code to start a voice recognition module

Here is the result of a string speech conversion result can be () configure the language by iatDialog.setParameter, the interval time (ie how long regarded as the end does not speak), etc., specifically refer to the official website of the document.

 1private void changeIntoText() {
 2
 3        // ②初始化有交互动画的语音识别器
 4        iatDialog = new RecognizerDialog(SearchMusicActivity.this, mInitListener);
 5        //③设置监听,实现听写结果的回调
 6        int a = 1+2;
 7
 8        iatDialog.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
 9        iatDialog.setParameter(SpeechConstant.ACCENT, "mandarin");
10
11        iatDialog.setListener(new RecognizerDialogListener() {
12            String resultJson = "[";//放置在外边做类的变量则报错,会造成json格式不对(?)
13
14            @Override
15            public void onResult(RecognizerResult recognizerResult, boolean isLast) {
16                System.out.println("-----------------   onResult   -----------------");
17                if (!isLast) {
18                    resultJson += recognizerResult.getResultString() + ",";
19                } else {
20                    resultJson += recognizerResult.getResultString() + "]";
21                }
22
23                if (isLast) {
24                    //解析语音识别后返回的json格式的结果
25                    Gson gson = new Gson();
26                    List<DictationResult> resultList = gson.fromJson(resultJson,
27                            new TypeToken<List<DictationResult>>() {
28                            }.getType());
29                    String result = "";
30                    for (int i = 0; i < resultList.size() - 1; i++) {
31                        result += resultList.get(i).toString();
32                    }
33
34                    et_content.setText(result);
35                    //获取焦点
36                    et_content.requestFocus();
37                    //将光标定位到文字最后,以便修改
38                    et_content.setSelection(result.length());
39                }
40            }
41
42            @Override
43            public void onError(SpeechError speechError) {
44                //自动生成的方法存根
45                speechError.getPlainDescription(true);
46            }
47        });
48        //开始听写,需将sdk中的assets文件下的文件夹拷入项目的assets文件夹下(没有的话自己新建)
49        iatDialog.show();
50    }
51
52    private InitListener mInitListener = new InitListener() {
53        @Override
54        public void onInit(int code) {
55            Log.d(TAG, "SpeechRecognizer init() code = " + code);
56            if (code != ErrorCode.SUCCESS) {
57                Toast.makeText(SearchMusicActivity.this, "初始化失败,错误码:" + code, Toast.LENGTH_SHORT).show();
58            }
59        }
60    };

【end】

◆有奖征文◆


推荐阅读2020年,5种将死的编程语言检测、量化、追踪新冠病毒,基于深度学习的自动CT图像分析有多靠谱?GitHub 接连封杀开源项目惹众怒,CEO 亲自道歉!智能合约编写之 Solidity 的设计模式低学历、文科出身,我如何从月薪不到 3000 逆袭为大厂高薪程序员?从提取层、处理层、基础结构入手,带你了解Spark和Kafka!你点的每个“在看”,我都认真当成了AI
Released 1366 original articles · won praise 10000 + · views 6.43 million +

Guess you like

Origin blog.csdn.net/dQCFKyQDXYm3F8rB0/article/details/105020639