科大讯飞语音服务-只含听写
想要集成科大讯飞语音服务首先要在人家的平台上注册一个自己的开发者账号
科大讯飞语音-讯飞开放平台地址:http://www.xfyun.cn/?ch=bdtg
进入地址之后要登录自己的账号,如果没有的话就自己注册一个账号
登陆之后点击控制台进入到我的语音云页面,在里面找到创建新应用
创建完成之后就开通自己所需要的一些服务
然后就是下载人家的SDK了,下载完成之后就将里面的libs包和assets分别导入到自己的项目里面,上面的导航栏上有SDK下载,点击进入选择自己想要完成的效果(目前这里只有听写服务)然后选择平台基于自己的开发平台选择,最后选择自己的应用,就是自己刚创建的新应用,点击下载SDK,我们所需要的东西都在下载的那个.zip文件里面了,先将libs里面的两个jar包导入到自己的工程里面,还有一个sample文件,里面有三个项目,可以导入到工程中运行看看效果
libs里面加入两个jar包
然后将剩下的文件添加到jniLibs里面,这个jniLibs放在自己的main里面
然后将assets整个文件拷到自己项目的main里面
然后就开始我们的代码………..
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.JSONArrayimport org.json.JSONObjectimport org.json.JSONTokenerpublic 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 // 如果需要多候选结果,解析数组其他字段 JSONArray items = words.getJSONObject(i).getJSONArray("cw") JSONObject obj = items.getJSONObject(0) ret.append(obj.getString("w"))// 如果需要多候选结果,解析数组其他字段// for(int j = 0// {// 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 JSONArray items = words.getJSONObject(i).getJSONArray("cw") for (int j = 0 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 JSONArray items = words.getJSONObject(i).getJSONArray("cw") for (int j = 0 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值
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(); }}
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() { RecognizerDialog mDialog = new RecognizerDialog(context, initListener); mDialog.setParameter(SpeechConstant.LANGUAGE, "zh_cn"); mDialog.setParameter(SpeechConstant.ACCENT, "mandarin"); mDialog.setListener(mRecognizerDialogListener); 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"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <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" /> <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