import com.iflytek.cloud.SpeechUtility;
public class SpeechApplication extends Application {
@Override
public void onCreate() {
// 5ef048e1 为在开放平台注册的APPID 注意没有空格,直接替换即可
SpeechUtility.createUtility(SpeechApplication.this, “appid=5ef048e1”);
super.onCreate();
}
}
打开AndroidManifest.xml,增加权限配置
配置SpeechApplication和注册权限
然后修改app模块下面的build.gradle
sourceSets {
main {
jniLibs.srcDirs = [‘libs’]
}
}
implementation files(‘libs/Msc.jar’)
改完记得Sync一下
然后修改布局activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:gravity=“center”
android:orientation=“vertical”
tools:context=“.MainActivity”>
<TextView
android:id=“@+id/tv_result”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“识别到的内容”
android:textColor=“#000” />
<Button
android:id=“@+id/btn_start”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginTop=“20dp”
android:text=“开始识别” />
接下来就是MainActivity了
③ 编码
一、声明变量和初始化
private static final String TAG = “MainActivity”;
private SpeechRecognizer mIat;// 语音听写对象
private RecognizerDialog mIatDialog;// 语音听写UI
// 用HashMap存储听写结果
private HashMap<String, String> mIatResults = new LinkedHashMap<String, String>();
private SharedPreferences