android上实现语音识别,基于google的语音识的简单例子.
16lz
2021-01-23
语音识别在android上使用起来很方便也很简单.
但是有个前提条件,就是android机器上必须预先安装google的语音搜索工具
语音识别技术是在Android SDK1.5中才加入的(RecognizerIntent),这里我们简单的分析一下自带的api例子,其实它就是通过一个Intent的Action动作来完成的。主要有以下两种模式:
ACTION_RECOGNIZE_SPEECH:一般语音识别,在这种模式下我们可以捕捉到语音的处理后的文字列。
ACTION_WEB_SEARCH:网络搜索
该例子同样是使用ACTION_RECOGNIZE_SPEECH模式,我们需要实现onActivityResult方法,当语音识别结束之后的回调函数。
下面是核心代码:
[java] view plain copy print ?
- package com.ifeisu.test.voice;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.content.pm.ResolveInfo;
- import android.os.Bundle;
- import android.speech.RecognizerIntent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.ListView;
- /***
- * 语音识别demo,在使用此demo之前需要确认手机或者模拟器上安装了google的语音搜索工具
- * Voice_Search_2.1.4.apk或者更高版本
- * @author guoxinzz@163.com
- * @website http://www.ifeisu.com
- *
- */
- public class VoiceRecognition extends Activity implements OnClickListener {
- private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
- private ListView mList;
- /**
- *
- * Called with the activity is first created.
- */
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button speakButton = (Button) findViewById(R.id.btn_speak);
- mList = (ListView) findViewById(R.id.list);
- // Check to see if a recognition activity is present
- PackageManager pm = getPackageManager();
- List
activities = pm.queryIntentActivities(new Intent( - RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
- if (activities.size() != 0)
- {
- speakButton.setOnClickListener(this);
- }
- else
- {
- speakButton.setEnabled(false);
- speakButton.setText("Recognizer not present");
- }
- }
- public void onClick(View v)
- {
- if (v.getId() == R.id.btn_speak)
- {
- startVoiceRecognitionActivity();
- }
- }
- private void startVoiceRecognitionActivity()
- {
- // 通过Intent传递语音识别的模式
- Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
- // 语言模式和自由形式的语音识别
- intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
- RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
- // 提示语音开始
- intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
- "请说出需要打开的网站名字.");
- // 开始执行我们的Intent、语音识别
- startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
- }
- // 当语音结束时的回调函数onActivityResult
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data)
- {
- if (requestCode == VOICE_RECOGNITION_REQUEST_CODE
- && resultCode == RESULT_OK)
- {
- // 取得语音的字符
- ArrayList
matches = data - .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
- mList.setAdapter(new ArrayAdapter
( this, - android.R.layout.simple_list_item_1, matches));
- }
- super.onActivityResult(requestCode, resultCode, data);
- }
- }
1.如果手机不支持语音搜索,则需要下载google的语音搜索工具包 Voice_Search_2.1.4.apk,这个可以到hiapk.com或者其他应用商店下载到.
2.源码下载:demo.voice_recognition.rar 因为csdn不让上传附件,在连接上右键->连接另存为,下载回去将名字改为demo.voice_recognition.rar就能正常打开了.
更多相关文章
- Android 启动模式
- 动画的两种实现模式Animation
- 如何修改Android设备添加重启、飞行模式、静音模式等功能(二)
- 装饰者模式及其应用
- 关于使用 Android MVVM + LiveData 模式的一些建议
- android语音识别方法示例代码
- 彻底分清Activity的启动模式,明白什么情况下应该使用哪种模式
- Android:Android涉及到的设计模式