Android开发之语音识别

开发背景

最近了解了一下Android Q(安卓10),得知Android Q基于语音识别技术,不需要联网就可以将视频中的语音实时转化成字幕这一功能。其实,在安卓中实现语音识别的代码很简单,我们要实现的就是调用谷歌API获取搜索结果,然后根据这个结果做自己想要的功能。
Android 中主要通过RecognizerIntent来实现语音识别。

RecognizerIntent相关知识

常量简介
ACTION_RECOGNIZE_SPEECH开启语音识别调用
ACTION_WEB_SEARCH开启网络语音识别,结果以网页的形式显示
EXTRA_LANGUAGE_MODEL语音识别的模式
EXTRA_MAX_RESULTS返回的最大结果
EXTRA_PROMPT设置用户可以开始语音的提示语
EXTRA_RESULTS将返回的字符串集中到一个list
LANGUAGE_MODEL_FREE_FORM在一种模式上的自由语音
LANGUAGE_MODEL_WEB_SEARCH使用语言模型在web上搜索
RESULT_AUDIO_ERROR返回结果时,音频遇到错误
RESULT_CLIENT_ERROR返回结果时,客户端遇到错误
RESULT_NETWORK_ERROR返回结果时,网络遇到错误
RESULT_NO_MATCH未知错误
RESULT_SERVER_ERROR返回结果时,服务端遇到错误

代码解释

1.​ 首先在AndroidManifest中设置网络权限,Android 6.0以后还需要动态请求权限

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

2.调用Google原生语音识别

  			@Override
            public void onClick(View v) {
                //开启语音识别功能
                Intent intent = new Intent(
                        RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                //设置模式,这里设置成自由模式
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                //提示语音开始文字
                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();
                }
            }
        });

3.使用onActivityResult接收返回的结果

  @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<String> 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<String> text = data
                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    //这里集合列表中第一个值为匹配度最高的值
                    txtText.setText(text.get(0));
                }
                break;
            }
        }
    }
}

2.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="fill_parent"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/txtText"
    android:gravity="center"
    android:orientation="vertical">

    <EditText
        android:id="@+id/txtText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="left"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:hint="@string/edit"/>

    <ImageButton
        android:id="@+id/btnSpeak"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:contentDescription="@string/speak"
        android:src="@android:drawable/ic_btn_speak_now"/>

</LinearLayout>

项目运行及问题解决

首先我们先把整个项目运行起来,运行结果如下图
在这里插入图片描述
接下来我们进行语音输入,发现语音输入用不了
在这里插入图片描述
为什么用不了呢?
通过网上查询得知原来使用语音识别功能需要语音识别程序(google voice)的支持,但是国内现在大部分厂商的手机里都没有这个程序。此外,这个语音识别方式需要联网到google的语音库中进行识别,因为google无法访问的问题也会导致识别失败。

知道了问题所在,解决起来也就是分分钟的事情了,首先先看一下实验所使用的模拟器,如下图
在这里插入图片描述
发现手机型号是国产的,在预设型号里面找了一下也都是国产手机型号。

现在的解决方法有两种,第一就是换一个可以支持语音识别程序的手机型号;第二就是直接下载语音识别程序附带到程序里面,显然第二种操作起来更加实际。
因为我所使用的模拟器可以联网,所以直接用模拟器自带的浏览器进行下载语音识别程序(google voice)
在这里插入图片描述
当然,如果你所使用的模拟器无法联网,也可以使用在Android emulator上安装 APK的方法进行下载安装 Google Voice Search APK。

安装完成后我们再次运行项目,就会发现当我们点击使用语音的时候就可以进行语音输入了。
在这里插入图片描述
进行语音输入的时候,模拟器要设置成可以接收语音,这样才能输入
在这里插入图片描述
参考文章:
1.Android-调用自带的谷歌语音识别技术
2.关于android上调用google的语音识别功能
3.在Android emulator上安装 APK

  • 5
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值