Android 輕鬆實現語音辨識詳解及執行個體代碼_Android

來源:互聯網
上載者:User

使用Intent調用語音辨識程式

說明

Android中主要通過RecognizerIntent來實現語音辨識,其實代碼比較簡單,但是如果找不到語音辨識裝置,就會拋出異常 ActivityNotFoundException,所以我們需要捕捉這個異常。而且語音辨識在模擬器上是無法測試的,因為語音辨識是訪問google 雲端資料,所以如果手機的網路沒有開啟,就無法實現識別聲音的!一定要開啟手機的網路,如果手機不存在語音辨識功能的話,也是無法啟用識別!

注意:使用前需要安裝語音辨識程式。如《語音搜尋》,其使用的語音辨識技術來自於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); // 網路識別程式    /*   * 此處沒有使用捕捉異常,而是檢測是否有語音辨識程式。   * 也可以在startRecognizerActivity()方法中捕捉ActivityNotFoundException異常   */  if (activities.size() != 0) {   btn.setOnClickListener(this);  } else {   // 若檢測不到語音辨識程式在本機安裝,測將扭銨置灰   btn.setEnabled(false);   btn.setText("未檢測到語音辨識裝置");  } } 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);  // 調出識別介面 } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {  // 回調擷取從Google得到的資料  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();  }  // 語音辨識後的回調,將識別的字串以Toast顯示  super.onActivityResult(requestCode, resultCode, data); }}

其主要原理就是將語音發送到google雲端,然後雲端處理,匹配相應的資料,發送到用戶端。

最後不要忘記,在manifest中加入網路存取權限:

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

運行後效果:

以上就是對Android 實現語音辨識的資料整理,後續繼續補充相關資料,謝謝大家對本站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.