switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && data != null) {
ArrayList text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
//这里集合列表中第一个值为匹配度最高的值
txtText.setText(text.get(0));
}
break;
}
}
}
完整代码
1.MainActivity.java代码
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.speech.RecognizerIntent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
protected static final int RESULT_SPEECH = 1;
private ImageButton btnSpeak;
private EditText txtText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtText = findViewById(R.id.txtText);
btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
btnSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//开启语音识别功能
Intent intent = new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//设置模式
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,“en-US”);
//提示语音开始文字
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,“Please start your voice”);
//开始进行语音识别,这里先检测手机(模拟器)是否支持语音识别并且捕获异常
try {
startActivityForResult(intent, RESULT_SPEECH);
txtText.setText("");
} catch (ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(),
“Opps! Your device doesn’t support Speech to Text”,
Toast.LENGTH_SHORT);
t.show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && data != null) {
ArrayList text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
//这里集合列表中第一个值为匹配度最高的值
txtText.setText(text.get(0));
}
break;
}
}
}
}
2.activity_main.xml代码
<?xml version="1.0" encoding="utf-8"?><LinearLayout
xml
ns:android=“http://schemas.android/apk/res/android”
xmlns:tools=“http://schemas.android/tools”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:layout_toLeftOf="@+id/txtText"
android:gravity=“center”
android:orientation=“vertical”>
资源分享
- 最新大厂面试专题
这个题库内容是比较多的,除了一些流行的热门技术面试题,如Kotlin,数据库,Java虚拟机面试题,数组,Framework ,混合跨平台开发,等
- 对应导图的Android高级工程师进阶系统学习视频
最近热门的,NDK,热修复,MVVM,源码等一系列系统学习视频都有!
l">
资源分享
- 最新大厂面试专题
这个题库内容是比较多的,除了一些流行的热门技术面试题,如Kotlin,数据库,Java虚拟机面试题,数组,Framework ,混合跨平台开发,等
[外链图片转存中…(img-zouSZo1z-1647756785329)]
- 对应导图的Android高级工程师进阶系统学习视频
最近热门的,NDK,热修复,MVVM,源码等一系列系统学习视频都有!
[外链图片转存中…(img-t4ayzV6Q-1647756785330)]
更多推荐
Android开发之语音识别,Android开发知识体系
发布评论