精华帖 (1) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-08-20
最后修改:2009-08-20
版权申明:http://yarin.iteye.com/blog/453262 语音识别技术是在Android SDK1.5中才加入的(RecognizerIntent),这里我们简单的分析一下自带的api例子,其实它就是通过一个Intent的Action动作来完成的。主要有以下两种模式:
ACTION_RECOGNIZE_SPEECH:一般语音识别,在这种模式下我们可以捕捉到语音的处理后的文字列。 ACTION_WEB_SEARCH:网络搜索
该例子同样是使用ACTION_RECOGNIZE_SPEECH模式,我们需要实现onActivityResult方法,当语音识别结束之后的回调函数。 好了,废话不多说,看下面的代码,我加入了注释,重要的本来就不多几行。
package com.example.android.apis.app; 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; import java.util.ArrayList; import java.util.List; 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.voice_recognition); 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<ResolveInfo> 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, "Speech recognition demo"); //开始执行我们的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<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, matches)); } super.onActivityResult(requestCode, resultCode, data); } }
当然这里需要设备的支持,所以模拟器是没有效果的! 实际上在没有设备时会跑出ActivityNotFoundException异常。
这个例子很简单,到此为止,谢谢! 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-08-20
不知道支不支持中文语音, 呵呵
|
|
返回顶楼 | |
发表时间:2009-08-20
lordhong 写道 不知道支不支持中文语音, 呵呵 要支持中文貌似还有点困难,有待研究! |
|
返回顶楼 | |
发表时间:2009-08-20
请问一下楼主需要什么设备呀。
我把这个程序装在G1上看不到效果 那个按钮是禁用的。 |
|
返回顶楼 | |
发表时间:2009-08-20
TTS倒是有个中文包, 试了下你好世界...还不错
|
|
返回顶楼 | |
发表时间:2009-08-20
这个要在连网的情况下才能使用,不然的话会提示连接失败,更遗憾的是,这个还不支持中文
|
|
返回顶楼 | |
发表时间:2009-08-21
最后修改:2009-08-21
lordhong大哥说的很好哦!
微软遵照COM的标准,提供了一套Speech API来对MSTTS进行编程。好像是Delphi的库,今天简单的看了一下,有时间了在继续研究,望lordhong大哥多指点一下: Delphi: CoInitialize(nil);// 初始化COM 库 OleCheck(CoCreateInstance(CLASS_VTxtAuto_,nil,CLSCTX_ALL,IID_IVTxtAuto,IVTxtAutoEx)); // 创建IVTxtAuto 接口 IVTxtAutoEx.Register(' ',' '); // 向服务器注册 IVTxtAutoEx.Set_Enabled(1);// 打开mstts IVTxtAutoEx.Set_Speed(150);// 设置语音速度 |
|
返回顶楼 | |
发表时间:2009-08-21
MS Speech做得还不错的, IBM以前的viaVoice也很好, 不过google买了几家公司, 也在开发, 呵呵
|
|
返回顶楼 | |
发表时间:2009-09-03
android上应该还没有tts吧
|
|
返回顶楼 | |
发表时间:2009-09-03
diyisoft 写道 android上应该还没有tts吧
早就有了... |
|
返回顶楼 | |