Android初学者之轻松实现语音识别
苹果的iphone有语音识别用的是Google的技术,做为Google力推的Android 自然会将其核心技术往Android 系统里面植入,并结合google 的云端技术将其发扬光大。
所以Google Voice Recognition在Android 的实现就变得极其轻松。
语音识别,借助于云端技术可以识别用户的语音输入,包括语音控制等技术,下面我们将利用Google 提供的Api 实现这一功能。
功能点为:通过用户语音将用户输入的语音识别出来,并打印在列表上。
所以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 |
013 | * <a href= "http://www.apache.org/licenses/LICENSE-2.0" rel= "nofollow" target= "_blank" >http://www.apache.org/licenses/LICENSE-2.0</a> |
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 |
031 | package com.example.android.apis.app; |
032 |
033 | |
034 |
035 | import com.example.android.apis.R; |
036 |
037 | |
038 |
039 | import android.app.Activity; |
040 |
041 | import android.content.Intent; |
042 |
043 | import android.content.pm.PackageManager; |
044 |
045 | import android.content.pm.ResolveInfo; |
046 |
047 | import android.os.Bundle; |
048 |
049 | import android.speech.RecognizerIntent; |
050 |
051 | import android.view.View; |
052 |
053 | import android.view.View.OnClickListener; |
054 |
055 | import android.widget.ArrayAdapter; |
056 |
057 | import android.widget.Button; |
058 |
059 | import android.widget.ListView; |
060 |
061 | |
062 |
063 | import java.util.ArrayList; |
064 |
065 | import java.util.List; |
066 |
067 | |
068 |
069 | /** |
070 |
071 | * Sample code that invokes the speech recognition intent API. |
072 |
073 | */ |
074 |
075 | public class VoiceRecognition extends Activity implements OnClickListener { |
076 |
077 | |
078 |
079 | private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234; |
080 |
081 | |
082 |
083 | private ListView mList; |
084 |
085 | |
086 |
087 | /** |
088 |
089 | * Called with the activity is first created. |
090 |
091 | */ |
092 |
093 | @Override |
094 |
095 | public void onCreate(Bundle savedInstanceState) { |
096 |
097 | super .onCreate(savedInstanceState); |
098 |
099 | |
100 |
101 | // Inflate our UI from its XML layout description. |
102 |
103 | setContentView(R.layout.voice_recognition); |
104 |
105 | |
106 |
107 | // Get display items for later interaction |
108 |
109 | Button speakButton = (Button) findViewById(R.id.btn_speak); |
110 |
111 | |
112 |
113 | mList = (ListView) findViewById(R.id.list); |
114 |
115 | |
116 |
117 | // Check to see if a recognition activity is present |
118 |
119 | PackageManager pm = getPackageManager(); |
120 |
121 | List activities = pm.queryIntentActivities( |
122 |
123 | new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); |
124 |
125 | if (activities.size() != 0) { |
126 |
127 | speakButton.setOnClickListener( this ); |
128 |
129 | } else { |
130 |
131 | speakButton.setEnabled( false ); |
132 |
133 | speakButton.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 |
147 | public void onClick(View v) { |
148 |
149 | if (v.getId() == R.id.btn_speak) { |
150 |
151 | startVoiceRecognitionActivity(); |
152 |
153 | } |
154 |
155 | } |
156 |
157 | |
158 |
159 | /** |
160 |
161 | * Fire an intent to start the speech recognition activity. |
162 |
163 | */ |
164 |
165 | private void startVoiceRecognitionActivity() { |
166 |
167 | Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); |
168 |
169 | intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, |
170 |
171 | RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); |
172 |
173 | intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo" ); |
174 |
175 | startActivityForResult(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 |
189 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
190 |
191 | if (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 |
195 | ArrayList matches = data.getStringArrayListExtra( |
196 |
197 | RecognizerIntent.EXTRA_RESULTS); |
198 |
199 | mList.setAdapter( new ArrayAdapter( this , android.R.layout.simple_list_item_1, |
200 |
201 | matches)); |
202 |
203 | } |
204 |
205 | |
206 |
207 | super .onActivityResult(requestCode, resultCode, data); |
208 |
209 | } |
210 |
211 | } |