Android 實現語音識別效果

fmms 12年前發布 | 82K 次閱讀 Android Android開發 移動開發

      前段時間,在google官方,看語音識別這里, 覺得挺有意思的,所以自己寫了一個小小的例子,和大家一起分享!注意如果手機的網絡沒有開啟,就無法實現識別聲音的!所以一定要開啟手機的網絡,如果手機不存在語音識別功能的話,就無法啟用識別!下面是activity中的代碼:

package com.zhangke.spring.sky.yuyin;

import java.util.ArrayList; import java.util.List;

import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.content.res.ColorStateList; import android.os.Bundle; import android.speech.RecognizerIntent; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; /**

  • android語言識別功能
  • 特別注意:如果手機有語言設別功能,請開啟網絡,因為系統會根據你的聲音數據到google云端獲取聲音數據
  • @author spring sky
  • Email vipa1888@163.com
  • QQ:840950105
  • My name :石明政 / public class MainActivity extends Activity implements OnClickListener{ private Button btn ; private ListView listView; private static final int REQUEST_CODE = 1;

    @Override public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
    
     btn = (Button) this.findViewById(R.id.btn);
     listView = (ListView) this.findViewById(R.id.listview);
    
     /**
      * 下面是判斷當前手機是否支持語音識別功能
      */
     PackageManager pm = getPackageManager();
     List<ResolveInfo> list = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
     if(list.size()!=0)
     {
         btn.setOnClickListener(this);
     }else{
         btn.setEnabled(false);
         btn.setText("當前語音識別設備不可用...");
     }
    
    

    }

    @Override public void onClick(View v) {

     if(v.getId()==R.id.btn)
     {
         /**
          * 啟動手機內置的語言識別功能
          */
         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,REQUEST_CODE);
     }
    

    }

    @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {

     /**
      * 回調獲取從谷歌得到的數據
      */
     if(requestCode==REQUEST_CODE&&resultCode==RESULT_OK)
     {
    
         List<String> list = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
         listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list)); //把數據顯示在listview中
     }
     super.onActivityResult(requestCode, resultCode, data);
    

    }

}</pre>layout中的代碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="實現語音識別效果"
        android:id="@+id/tvTitle"
    />
    <Button  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="實現語音識別效果"
        android:id="@+id/btn"
    />
      <ListView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/listview"
    />
</LinearLayout>
這個功能不需要任何權限,只要確保手機有網絡就可以了!
轉自:http://blog.csdn.net/vipa1888/article/details/7023928

 本文由用戶 fmms 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!