安卓语音识别(RecognizerIntent)

今天在csdn网站上突然找到安卓语音识别功能感觉不错,结合其它博主和关于安卓语音识别的书籍来编写如下(若有侵犯,请立即告诉我)
话不多说直接进入主题:
Android主要通过自带的RecognizerIntent来实现语音识别,代码其实是很简单,但是如果没有找到语音设备就会抛出异常ActivityNotFoundException,所以我们要提前要捕捉这个异常。在这里说明一下语音识别好像在模拟器上不能测试(我观看了大神的讲解说,语音识别是访问google云端数据)如果手机的网络没有开启,就无法识别语音,所以一定要开启手机的网络,如果手机没有识别语音功能,也是不能的。
如果手机没有开启网络就会出现如下所示:
在这里插入图片描述
接下来话不多上直接上案例:
首先需要创建一个安卓项目(在这里我就不介绍怎么创建了,我相信大家都会)。
在xml中编写如下代码:

<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="160dp"
        android:layout_marginLeft="160dp"
        android:layout_marginTop="396dp"
        android:text="开始语音"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="220dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

就是编写了一个按钮和一个文本框。
在java找主要编写如下代码:

public class MainActivity extends AppCompatActivity {
   private Button button;
   private TextView textView;
   private static final int VOICE_RECOGNITION_REQUEST_CODE=123;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    //通过Intent传递语音识别的模式,开启语音
                    Intent intent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                    //语言模式和自由模式的语音识别    EXTRA_LANGUAGE_MODEL:语言模式  LANGUAGE_MODEL_FREE_FORM:自由模式语言
                    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                    //提示语音开始
                    intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"开始语音");
                    //开始语音识别
                    startActivityForResult(intent,VOICE_RECOGNITION_REQUEST_CODE);//打开语音识别模式
                }catch (Exception e){
                         e.printStackTrace();
                    Toast.makeText(getApplicationContext(),"找不到语音设备",Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
           //回调谷歌获取得到的数据
        if(requestCode==VOICE_RECOGNITION_REQUEST_CODE && resultCode==RESULT_OK){
            ////取得语音的字符
            ArrayList<String> results=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            String resultString="";
            for (int i=0;i<results.size();i++){
                resultString+=results.get(i);
            }
            Toast.makeText(this,resultString,Toast.LENGTH_SHORT).show();
            textView=(TextView)findViewById(R.id.textView);
            textView.setText(resultString);
            }
        }

}

其主要原理就是将语音发送到google云端,然后云端处理,匹配相应的数据,发送到客户端。
最后不要忘记,在manifest中加入网络访问权限:

<uses-permission android:name="android.permission.INTERNET" />

这个网络访问权限我之前没加也能出来相应的结果不知道怎么回事(

若有知道的大佬请告诉我吆。)
下面展示效果图:
在这里插入图片描述
在这里插入图片描述
最后就简单的完成了语音识别这个小案例。

原创文章 16 获赞 18 访问量 5074

猜你喜欢

转载自blog.csdn.net/jzdcuccess/article/details/106068433
今日推荐