Android轻松实现语音识别

移动开发 Android
iphone好像永远都在和Android相比较,本文是Android系统的语音识别。

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

所以Google Voice Recognition在Android 的实现就变得极其轻松。

[[31167]]

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

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

功能界面如下:

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

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

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

 

#p#

完整的代码如下:

  1. /*   
  2.  * Copyright (C) 2008 The Android Open Source Project  
  3.  *  
  4.  * Licensed under the Apache License, Version 2.0 (the "License");  
  5.  * you may not use this file except in compliance with the License.  
  6.  * You may obtain a copy of the License at  
  7.  *  
  8.  *      http://www.apache.org/licenses/LICENSE-2.0  
  9.  *  
  10.  * Unless required by applicable law or agreed to in writing, software  
  11.  * distributed under the License is distributed on an "AS IS" BASIS,  
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  13.  * See the License for the specific language governing permissions and  
  14.  * limitations under the License.  
  15.  */  
  16. package com.example.android.apis.app;  
  17. import com.example.android.apis.R;  
  18. import android.app.Activity;  
  19. import android.content.Intent;  
  20. import android.content.pm.PackageManager;  
  21. import android.content.pm.ResolveInfo;  
  22. import android.os.Bundle;  
  23. import android.speech.RecognizerIntent;  
  24. import android.view.View;  
  25. import android.view.View.OnClickListener;  
  26. import android.widget.ArrayAdapter;  
  27. import android.widget.Button;  
  28. import android.widget.ListView;  
  29. import java.util.ArrayList;  
  30. import java.util.List;  
  31. /**  
  32.  * Sample code that invokes the speech recognition intent API.  
  33.  */  
  34. public class VoiceRecognition extends Activity implements OnClickListener {     
  35.     private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;  
  36.       
  37.     private ListView mList;  
  38.  
  39.     /**  
  40.      * Called with the activity is first created.  
  41.      */  
  42.     @Override  
  43.     public void onCreate(Bundle savedInstanceState) {  
  44.         super.onCreate(savedInstanceState);  
  45.  
  46.         // Inflate our UI from its XML layout description.  
  47.         setContentView(R.layout.voice_recognition);  
  48.  
  49.         // Get display items for later interaction  
  50.         Button speakButton = (Button) findViewById(R.id.btn_speak);  
  51.           
  52.         mList = (ListView) findViewById(R.id.list);  
  53.  
  54.         // Check to see if a recognition activity is present  
  55.         PackageManager pm = getPackageManager();  
  56.         List<ResolveInfo> activities = pm.queryIntentActivities(  
  57.                 new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);  
  58.         if (activities.size() != 0) {  
  59.             speakButton.setOnClickListener(this);  
  60.         } else {  
  61.             speakButton.setEnabled(false);  
  62.             speakButton.setText("Recognizer not present");  
  63.         }  
  64.     }  
  65.     /**  
  66.      * Handle the click on the start recognition button.  
  67.      */  
  68.     public void onClick(View v) {  
  69.         if (v.getId() == R.id.btn_speak) {  
  70.             startVoiceRecognitionActivity();  
  71.         }  
  72.     }  
  73.     /**  
  74.      * Fire an intent to start the speech recognition activity.  
  75.      */  
  76.     private void startVoiceRecognitionActivity() {  
  77.         Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);  
  78.         intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,  
  79.                 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);  
  80.         intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");  
  81.         startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);  
  82.     }  
  83.     /**  
  84.      * Handle the results from the recognition activity.  
  85.      */  
  86.     @Override  
  87.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  88.         if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {  
  89.             // Fill the list view with the strings the recognizer thought it could have heard  
  90.             ArrayList<String> matches = data.getStringArrayListExtra(  
  91.                     RecognizerIntent.EXTRA_RESULTS);  
  92.             mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,  
  93.                     matches));  
  94.         }  
  95.         super.onActivityResult(requestCode, resultCode, data);  
  96.     }  

【编辑推荐】

Android 广播接收者

Android 目录结构分析

Android应用程序开发环境的搭建

在Android应用程序中使用Internet数据

责任编辑:zhaolei 来源: 网络转载
相关推荐

2012-07-10 13:29:30

Java

2015-12-07 14:39:18

微软Windows 95语音识别

2012-07-25 13:23:32

ibmdw

2017-01-06 13:12:11

AndroidRecyclerVie悬浮条

2016-02-17 10:39:18

语音识别语音合成语音交互

2022-09-26 11:35:50

App开发技术框架Speech框架

2011-01-18 11:52:25

Linux语音识别

2021-05-06 11:13:06

人工智能语音识别

2009-07-21 15:28:06

Windows Emb

2021-05-06 11:18:23

人工智能语音识别

2009-08-21 15:28:23

C#英文

2021-12-24 10:34:11

鸿蒙HarmonyOS应用

2022-12-01 07:03:22

语音识别人工智能技术

2017-02-27 21:37:49

2019-10-12 17:42:33

2021-11-17 10:37:39

语音识别技术人工智能

2017-10-27 16:19:23

语音识别CNN

2015-10-23 13:41:20

android源码科大讯飞语音识别

2017-03-20 10:14:03

语音识别匹配算法模型

2024-01-08 19:30:15

AI开源语音识别
点赞
收藏

51CTO技术栈公众号