android 语音识别技术
偶然在网上看到。于是照着网上的例子写了一个例子。很简单,就是实现语音识别。后来的TTS没有做。
直接贴代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/** * 语音识别 * * @author sy * */ public class SpeakToActivity extends Activity { /** Called when the activity is first created. */ private Button mSpeakButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mSpeakButton = (Button) findViewById(R.id.openSpeak); mSpeakButton.setOnClickListener(new ButtonOnClick()); } /** * button点击事件 * * @author sy * */ class ButtonOnClick implements OnClickListener { @Override public void onClick(View v) { try { // TODO Auto-generated method stub Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); // 使用Intent开始语音识别 // //语言模式和自由模式的语音识别 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "开始语音");// 只是提示语音开始 startActivityForResult(intent, 10);// 对应onActivityResult方法中得requestCode } catch (Exception e) { e.printStackTrace(); // 如果找不到语音设备则提示 Toast.makeText(SpeakToActivity.this, "找不到语音设备", Toast.LENGTH_LONG).show(); } } } /** * activity回调函数,和startActivityForResult对应 * * @author sy */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (requestCode == 10 && resultCode == RESULT_OK) { ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); // 获取结果集 String resultString = ""; // 如果使用这段注释的代码,google则会返回5个类似于你发音的词 // for (int i = 0; i < results.size(); i++) // { // resultString+=results.get(i); // } resultString += results.get(0);// 我默认选择了第一个 Toast.makeText(SpeakToActivity.this, resultString, Toast.LENGTH_LONG).show(); } } }