欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 輕松實(shí)現(xiàn)語音識別詳解及實(shí)例代碼

 更新時(shí)間:2016年09月18日 10:21:31   作者:麥兜LK  
這篇文章主要介紹了Android 輕松實(shí)現(xiàn)語音識別的相關(guān)資料,并附實(shí)例代碼,需要的朋友可以參考下

使用Intent調(diào)用語音識別程序

說明

Android中主要通過RecognizerIntent來實(shí)現(xiàn)語音識別,其實(shí)代碼比較簡單,但是如果找不到語音識別設(shè)備,就會拋出異常 ActivityNotFoundException,所以我們需要捕捉這個(gè)異常。而且語音識別在模擬器上是無法測試的,因?yàn)檎Z音識別是訪問google 云端數(shù)據(jù),所以如果手機(jī)的網(wǎng)絡(luò)沒有開啟,就無法實(shí)現(xiàn)識別聲音的!一定要開啟手機(jī)的網(wǎng)絡(luò),如果手機(jī)不存在語音識別功能的話,也是無法啟用識別!

注意:使用前需要安裝語音識別程序。如《語音搜索》,其使用的語音識別技術(shù)來自于Google,Intent可以識別到該程序。

本例參考自android例程:

development/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.java

核心代碼及說明

package com.example.test;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements OnClickListener {
 private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Button btn = (Button) findViewById(R.id.btn); // 識別按鈕
  PackageManager pm = getPackageManager();
  List activities = pm.queryIntentActivities(new Intent(
    RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); // 本地識別程序
  // new Intent(RecognizerIntent.ACTION_WEB_SEARCH), 0); // 網(wǎng)絡(luò)識別程序
  
  /*
   * 此處沒有使用捕捉異常,而是檢測是否有語音識別程序。
   * 也可以在startRecognizerActivity()方法中捕捉ActivityNotFoundException異常
   */
  if (activities.size() != 0) {
   btn.setOnClickListener(this);
  } else {
   // 若檢測不到語音識別程序在本機(jī)安裝,測將扭銨置灰
   btn.setEnabled(false);
   btn.setText("未檢測到語音識別設(shè)備");
  }
 }

 public void onClick(View v) {
  if (v.getId() == R.id.btn) {
   startRecognizerActivity();
  }
 }
 // 開始識別
 private void startRecognizerActivity() { 
  // 通過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, "開始語音");
  // 開始語音識別
  startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
  // 調(diào)出識別界面
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  // 回調(diào)獲取從谷歌得到的數(shù)據(jù)
  if (requestCode == VOICE_RECOGNITION_REQUEST_CODE
    && resultCode == RESULT_OK) {
   // 取得語音的字符
   ArrayList<String> results = data
     .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
   String resultString = "";
   for (int i = 0; i < results.size(); i++) {
    resultString += results.get(i);
   }
   Toast.makeText(this, resultString, Toast.LENGTH_SHORT).show();
  }
  // 語音識別后的回調(diào),將識別的字串以Toast顯示
  super.onActivityResult(requestCode, resultCode, data);
 }
}

其主要原理就是將語音發(fā)送到google云端,然后云端處理,匹配相應(yīng)的數(shù)據(jù),發(fā)送到客戶端。

最后不要忘記,在manifest中加入網(wǎng)絡(luò)訪問權(quán)限:

<uses-permission android:name="android.permission.INTERNET" />

運(yùn)行后效果:

以上就是對Android 實(shí)現(xiàn)語音識別的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對本站的支持!

相關(guān)文章

最新評論