不能用中文的语音识别感觉很不爽,下午终于弄出来了,网上查找资料说要使用开源项目eyes-free或者下载科大讯飞的 jar 包。但我是用谷歌的 voice search (现在有支持中文了)来做的。
名词解释
语音搜索(voice search)是一种语音识别技术,它使用户能够通过大声说出条目来进行搜索,而不用将它们输入到搜索栏来搜索。智能手机以及其他小型的具有网络功能的移动设备的剧增激发了大家对语音搜索的兴趣。
语音搜索(voice search)的应用包括:
- 进行搜索引擎查询。
- 明确具体的要求。
- 请求具体信息,如股票报价。
- 启动程序并选择选项。
- 寻找音频或视频文件里的内容。
- 语音拨号。
语音搜索(voice search)通常是一个应用软件,但它也可以是一个服务。语音搜索应用程序,如具有语音功能的谷歌移动和iPhone的Vlingo,依靠的是语音识别程序。
显示:
布局代码:
<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_above="@+id/textView1"
android:layout_toLeftOf="@+id/textView1"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="@+id/btnSpeak"
android:text="button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/txtText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
package com.example.speechtotextdemo;
import java.util.ArrayList;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int VOICE_RECOGNITION = 83; //startActivityForResult操作要求的标识码
private Button btnSpeak;
private TextView txtText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
txtText = (TextView) findViewById(R.id.txtText);
btnSpeak = (Button) 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,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
"Speech recognition demo"); //设置语音识别Intent调用的特定属性参数
try {
//启动一个要求有返回值的activity调用
startActivityForResult(intent, VOICE_RECOGNITION);
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) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case VOICE_RECOGNITION: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtText.setText(text.get(0));
}
break;
}
}
}
}