Android实现语音识别 .

本文介绍如何在Android应用中实现语音识别功能,通过调用Google提供的API,用户可以通过语音输入进行操作,文章提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

苹果的iphone的语音识别功能使用的是Google的技术,做为Google力推的Android自然会将其核心技术植入到Android系统里面,并结合google的云端技术将其发扬光大。

所以Google Voice Recognition在Android中的实现就变得非常轻松。

Android语音识别,借助于云端技术可以识别用户的语音输入,包括语音控制等技术,下面我们将利用Google提供的Api实现这一功能。

功能点为:通过用户语音将用户输入的语音识别出来,并打印在列表上。

功能界面如下:


用户通过点击speak按钮显示界面:


用户说完话后,将提交到云端搜索:


在云端搜索完成后,返回打印数据:


完整代码如下:

  1. package com.example.android.apis.app;  
  2.    
  3. import com.example.android.apis.R;  
  4.    
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.content.pm.PackageManager;  
  8. import android.content.pm.ResolveInfo;  
  9. import android.os.Bundle;  
  10. import android.speech.RecognizerIntent;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.ArrayAdapter;  
  14. import android.widget.Button;  
  15. import android.widget.ListView;  
  16.    
  17. import java.util.ArrayList;  
  18. import java.util.List;  
  19.    
  20. /** 
  21.  * Sample code that invokes the speech recognition intent API. 
  22.  */  
  23. public class VoiceRecognition extends Activity implements OnClickListener {  
  24.    
  25.     private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;  
  26.    
  27.     private ListView mList;  
  28.    
  29.     /** 
  30.      * Called with the activity is first created. 
  31.      */  
  32.     @Override  
  33.     public void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);  
  35.    
  36.         // Inflate our UI from its XML layout description.   
  37.         setContentView(R.layout.voice_recognition);  
  38.    
  39.         // Get display items for later interaction   
  40.         Button speakButton = (Button) findViewById(R.id.btn_speak);  
  41.    
  42.         mList = (ListView) findViewById(R.id.list);  
  43.    
  44.         // Check to see if a recognition activity is present   
  45.         PackageManager pm = getPackageManager();  
  46.         List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(  
  47.                 RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);  
  48.         if (activities.size() != 0) {  
  49.             speakButton.setOnClickListener(this);  
  50.         } else {  
  51.             speakButton.setEnabled(false);  
  52.             speakButton.setText("Recognizer not present");  
  53.         }  
  54.     }  
  55.    
  56.     /** 
  57.      * Handle the click on the start recognition button. 
  58.      */  
  59.     public void onClick(View v) {  
  60.         if (v.getId() == R.id.btn_speak) {  
  61.             startVoiceRecognitionActivity();  
  62.         }  
  63.     }  
  64.    
  65.     /** 
  66.      * Fire an intent to start the speech recognition activity. 
  67.      */  
  68.     private void startVoiceRecognitionActivity() {  
  69.         Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);  
  70.         intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,  
  71.                 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);  
  72.         intent.putExtra(RecognizerIntent.EXTRA_PROMPT,  
  73.                 "Speech recognition demo");  
  74.         startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);  
  75.     }  
  76.    
  77.     /** 
  78.      * Handle the results from the recognition activity. 
  79.      */  
  80.     @Override  
  81.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  82.         if (requestCode == VOICE_RECOGNITION_REQUEST_CODE  
  83.                 && resultCode == RESULT_OK) {  
  84.             // Fill the list view with the strings the recognizer thought it   
  85.             // could have heard   
  86.             ArrayList<String> matches = data  
  87.                     .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);  
  88.             mList.setAdapter(new ArrayAdapter<String>(this,  
  89.                     android.R.layout.simple_list_item_1, matches));  
  90.         }  
  91.    
  92.         super.onActivityResult(requestCode, resultCode, data);  
  93.     }  
  94. }  
package com.example.android.apis.app;
 
import com.example.android.apis.R;
 
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
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;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * Sample code that invokes the speech recognition intent API.
 */
public class VoiceRecognition extends Activity implements OnClickListener {
 
    private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
 
    private ListView mList;
 
    /**
     * Called with the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        // Inflate our UI from its XML layout description.
        setContentView(R.layout.voice_recognition);
 
        // Get display items for later interaction
        Button speakButton = (Button) findViewById(R.id.btn_speak);
 
        mList = (ListView) findViewById(R.id.list);
 
        // Check to see if a recognition activity is present
        PackageManager pm = getPackageManager();
        List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
                RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
        if (activities.size() != 0) {
            speakButton.setOnClickListener(this);
        } else {
            speakButton.setEnabled(false);
            speakButton.setText("Recognizer not present");
        }
    }
 
    /**
     * Handle the click on the start recognition button.
     */
    public void onClick(View v) {
        if (v.getId() == R.id.btn_speak) {
            startVoiceRecognitionActivity();
        }
    }
 
    /**
     * Fire an intent to start the speech recognition activity.
     */
    private void startVoiceRecognitionActivity() {
        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");
        startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
    }
 
    /**
     * Handle the results from the recognition activity.
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == VOICE_RECOGNITION_REQUEST_CODE
                && resultCode == RESULT_OK) {
            // Fill the list view with the strings the recognizer thought it
            // could have heard
            ArrayList<String> matches = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            mList.setAdapter(new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, matches));
        }
 
        super.onActivityResult(requestCode, resultCode, data);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值