Android初学者之轻松实现语音识别


Android初学者之轻松实现语音识别

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

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

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

功能点为:通过用户语音将用户输入的语音识别出来,并打印在列表上。 
001* Copyright (C) 2008 The Android Open Source Project
002 
003*
004 
005* Licensed under the Apache License, Version 2.0 (the "License");
006 
007* you may not use this file except in compliance with the License.
008 
009* You may obtain a copy of the License at
010 
011*
012 
014 
015*
016 
017* Unless required by applicable law or agreed to in writing, software
018 
019* distributed under the License is distributed on an "AS IS" BASIS,
020 
021* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
022 
023* See the License for the specific language governing permissions and
024 
025* limitations under the License.
026 
027*/
028 
029  
030 
031package com.example.android.apis.app;
032 
033  
034 
035import com.example.android.apis.R;
036 
037  
038 
039import android.app.Activity;
040 
041import android.content.Intent;
042 
043import android.content.pm.PackageManager;
044 
045import android.content.pm.ResolveInfo;
046 
047import android.os.Bundle;
048 
049import android.speech.RecognizerIntent;
050 
051import android.view.View;
052 
053import android.view.View.OnClickListener;
054 
055import android.widget.ArrayAdapter;
056 
057import android.widget.Button;
058 
059import android.widget.ListView;
060 
061  
062 
063import java.util.ArrayList;
064 
065import java.util.List;
066 
067  
068 
069/**
070 
071* Sample code that invokes the speech recognition intent API.
072 
073*/
074 
075public class VoiceRecognition extends Activity implements OnClickListener {
076 
077  
078 
079private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
080 
081  
082 
083private ListView mList;
084 
085  
086 
087/**
088 
089* Called with the activity is first created.
090 
091*/
092 
093@Override
094 
095public void onCreate(Bundle savedInstanceState) {
096 
097super.onCreate(savedInstanceState);
098 
099  
100 
101// Inflate our UI from its XML layout description.
102 
103setContentView(R.layout.voice_recognition);
104 
105  
106 
107// Get display items for later interaction
108 
109Button speakButton = (Button) findViewById(R.id.btn_speak);
110 
111  
112 
113mList = (ListView) findViewById(R.id.list);
114 
115  
116 
117// Check to see if a recognition activity is present
118 
119PackageManager pm = getPackageManager();
120 
121List activities = pm.queryIntentActivities(
122 
123new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
124 
125if (activities.size() != 0) {
126 
127speakButton.setOnClickListener(this);
128 
129} else {
130 
131speakButton.setEnabled(false);
132 
133speakButton.setText("Recognizer not present");
134 
135}
136 
137}
138 
139  
140 
141/**
142 
143* Handle the click on the start recognition button.
144 
145*/
146 
147public void onClick(View v) {
148 
149if (v.getId() == R.id.btn_speak) {
150 
151startVoiceRecognitionActivity();
152 
153}
154 
155}
156 
157  
158 
159/**
160 
161* Fire an intent to start the speech recognition activity.
162 
163*/
164 
165private void startVoiceRecognitionActivity() {
166 
167Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
168 
169intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
170 
171RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
172 
173intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
174 
175startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
176 
177}
178 
179  
180 
181/**
182 
183* Handle the results from the recognition activity.
184 
185*/
186 
187@Override
188 
189protected void onActivityResult(int requestCode, int resultCode, Intent data) {
190 
191if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
192 
193// Fill the list view with the strings the recognizer thought it could have heard
194 
195ArrayList matches = data.getStringArrayListExtra(
196 
197RecognizerIntent.EXTRA_RESULTS);
198 
199mList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,
200 
201matches));
202 
203}
204 
205  
206 
207super.onActivityResult(requestCode, resultCode, data);
208 
209}
210 
211}
评价:

分享 45 分钟前
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值