如何在 Android 应用程序中使用 SpeechRecognizer API?
此示例演示了如何在 Android 中使用 SpeechRecognizer API。
步骤 1 − 在 Android Studio 中创建新项目,转到文件 ⇒ 新建项目并填写所有必需详细信息以创建新项目。
步骤 2 − 将以下代码添加到 res/layout/activity_main.xml。
<?xml version="1.0" encoding="utf-8"?> <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" android:padding="16sp" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="40dp" android:textSize="16sp" android:textStyle="bold" /> </LinearLayout>
步骤 3 − 将以下代码添加到 src/MainActivity.java
package app.com.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.widget.TextView;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private final int REQUEST_SPEECH_RECOGNIZER = 3000;
private TextView textView;
private final String mQuestion = "Which company is the largest online retailer on the planet?";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
startSpeechRecognizer();
}
private void startSpeechRecognizer() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, mQuestion);
startActivityForResult(intent, REQUEST_SPEECH_RECOGNIZER);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_SPEECH_RECOGNIZER) {
if (resultCode == RESULT_OK) {
List<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String mAnswer = results.get(0);
if (mAnswer.toUpperCase().contains("AMAZON"))
textView.setText(String.format("
Question: %s
Your answer is '%s' and it is
correct!", mQuestion, mAnswer));
else
textView.setText(String.format("
Question: %s
Your answer is '%s' and it is
incorrect!", mQuestion, mAnswer));
}
}
}
}
步骤 4 − 将以下代码添加到 androidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" 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> <uses-permission android:name="android.permission.RECORD_AUDIO" /> </manifest>
我们来尝试运行你的应用程序。我假设你已经将实际的 Android 移动设备连接到你的计算机。要从 Android Studio 运行应用程序,请打开一个属于你的项目的文件并点击工具栏上的运行
图标。选择你的移动设备作为选项,然后检查你的移动设备,它会显示你的默认屏幕 −


广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP