Android语音识别--RecognizerIntent
在网上找了一篇关于Android语音识别的文章,自己慢慢研究。
Android中主要通过RecognizerIntent来实现语音识别,其实代码比较简单,但是如果找不到设置,就会抛出异常 ActivityNotFoundException,所以我们需要捕捉这个异常。而且语音识别在模拟器上是无法测试的,因为语音识别是访问google 云端数据,所以如果手机的网络没有开启,就无法实现识别声音的!一定要开启手机的网络,如果手机不存在语音识别功能的话,也是无法启用识别!
RecognizerIntent的一些常量:
其主要原理就是将语音发送到google云端,然后云端处理,匹配相应的数据,发送到客户端。
最后不要忘记,在manifest中加入网络访问权限:
<uses-permission android:name="android.permission.INTERNET" />
运行后效果:
另一个代码:
Android中主要通过RecognizerIntent来实现语音识别,其实代码比较简单,但是如果找不到设置,就会抛出异常 ActivityNotFoundException,所以我们需要捕捉这个异常。而且语音识别在模拟器上是无法测试的,因为语音识别是访问google 云端数据,所以如果手机的网络没有开启,就无法实现识别声音的!一定要开启手机的网络,如果手机不存在语音识别功能的话,也是无法启用识别!
RecognizerIntent的一些常量:
我们只需要通过Intent来传递一个动作以及一些属性,然后通过startActivityForResult来开始语音,代码如下:
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"开始语音");
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
01 |
public class
RecognizerIntentActivity extends
Activity { |
02 |
03 |
private
Button btnReconizer; |
04 |
private
static final int
VOICE_RECOGNITION_REQUEST_CODE =
1234 ; |
05 |
@Override |
06 |
protected
void onCreate(Bundle savedInstanceState)
{ |
07 |
//
TODO Auto-generated method stub |
08 |
super .onCreate(savedInstanceState); |
09 |
setContentView(R.layout.reconizer); |
10 |
|
11 |
btnReconizer=(Button)
this .findViewById(R.id.btnRecognizer); |
12 |
btnReconizer.setOnClickListener( new
OnClickListener() { |
13 |
|
14 |
@Override |
15 |
public
void onClick(View v) { |
16 |
//
TODO Auto-generated method stub |
17 |
try { |
18 |
//通过Intent传递语音识别的模式,开启语音 |
19 |
Intent
intent= new
Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); |
20 |
//语言模式和自由模式的语音识别 |
21 |
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); |
22 |
//提示语音开始 |
23 |
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
"开始语音" ); |
24 |
//开始语音识别 |
25 |
startActivityForResult(intent,
VOICE_RECOGNITION_REQUEST_CODE); |
26 |
} catch
(Exception e) { |
27 |
//
TODO: handle exception |
28 |
e.printStackTrace(); |
29 |
Toast.makeText(getApplicationContext(),
"找不到语音设备" ,
1 ).show(); |
30 |
} |
31 |
} |
32 |
}); |
33 |
|
34 |
} |
35 |
|
36 |
@Override |
37 |
protected
void onActivityResult( int
requestCode, int resultCode, Intent
data) { |
38 |
//
TODO Auto-generated method stub |
39 |
//回调获取从谷歌得到的数据 |
40 |
if (requestCode==VOICE_RECOGNITION_REQUEST_CODE
&&
resultCode==RESULT_OK){ |
41 |
//取得语音的字符 |
42 |
ArrayList<String>
results=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); |
43 |
|
44 |
String
resultString= "" ; |
45 |
for ( int
i= 0 ;i<results.size();i++){ |
46 |
resultString+=results.get(i); |
47 |
} |
48 |
Toast.makeText( this ,
resultString, 1 ).show(); |
49 |
} |
50 |
super .onActivityResult(requestCode,
resultCode, data); |
51 |
} |
52 |
} |
最后不要忘记,在manifest中加入网络访问权限:
<uses-permission android:name="android.permission.INTERNET" />
运行后效果:
![Android语音识别--RecognizerIntent用法](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
另一个代码:
- public class Activity01 extends Activity
- {
- private static final int VOICE_RECOGNITION_REQUEST_CODE = 4321;
- private ListView mList;
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mList = (ListView) findViewById(R.id.ListView01);
- Button button = (Button) findViewById(R.id.Button01);
- button.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- try
- {
- //通过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);
- }
- catch (ActivityNotFoundException e)
- {
- //找不到语音设备装置
- Toast.makeText(Activity01.this,
- "ActivityNotFoundException",
- Toast.LENGTH_LONG).show();
- }
- }
- });
- }
- //当语音结束时的回调函数onActivityResult
- @Override
- 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);
- //设置视图更新
- //mList.setAdapter(new ArrayAdapter<String>(this,android.
- R.layout.simple_list_item_1,results));
- String resultsString = "";
- for (int i = 0; i < results.size(); i++)
- {
- resultsString += results.get(i);
- }
- Toast.makeText(this,resultsString,Toast.LENGTH_LONG).show();
- super.onActivityResult(requestCode, resultCode, data);
- }
- }
- }