Android 轻松实现语音识别的完整代码.doc_第1页
Android 轻松实现语音识别的完整代码.doc_第2页
Android 轻松实现语音识别的完整代码.doc_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

代码Android 轻松实现语音识别的完整代码view sourceprint?001* Copyright (C)2008The Android Open Source Project002*003* Licensed under the Apache License, Version2.0(theLicense);004* you may not usethisfile except in compliance with the License.005* You may obtain a copy of the License at006*007* /licenses/LICENSE-2.0008*009* Unless required by applicable law or agreed to in writing, software010* distributed under the License is distributed on anAS ISBASIS,011* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.012* See the Licenseforthe specific language governing permissions and013* limitations under the License.014*/015016packagecom.example.android.apis.app;017018importcom.example.android.apis.R;019020importandroid.app.Activity;021importandroid.content.Intent;022importandroid.content.pm.PackageManager;023importandroid.content.pm.ResolveInfo;024importandroid.os.Bundle;025importandroid.speech.RecognizerIntent;026importandroid.view.View;027importandroid.view.View.OnClickListener;028importandroid.widget.ArrayAdapter;029importandroid.widget.Button;030importandroid.widget.ListView;031032importjava.util.ArrayList;033importjava.util.List;034035/*036* Sample code that invokes the speech recognition intent API.037*/038publicclassVoiceRecognitionextendsActivityimplementsOnClickListener 039040privatestaticfinalintVOICE_RECOGNITION_REQUEST_CODE =1234;041042privateListView mList;043044/*045* Called with the activity is first created.046*/047Override048publicvoidonCreate(Bundle savedInstanceState) 049super.onCreate(savedInstanceState);050051/ Inflate our UI from its XML layout description.052setContentView(R.layout.voice_recognition);053054/ Get display items for later interaction055Button speakButton = (Button) findViewById(R.id.btn_speak);056057mList = (ListView) findViewById(R.id.list);058059/ Check to see if a recognition activity is present060PackageManager pm = getPackageManager();061List activities = pm.queryIntentActivities(062newIntent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),0);063if(activities.size() !=0) 064speakButton.setOnClickListener(this);065else066speakButton.setEnabled(false);067speakButton.setText(Recognizer not present);068069070071/*072* Handle the click on the start recognition button.073*/074publicvoidonClick(View v) 075if(v.getId() = R.id.btn_speak) 076startVoiceRecognitionActivity();077078079080/*081* Fire an intent to start the speech recognition activity.082*/083privatevoidstartVoiceRecognitionActivity() 084Intent intent =newIntent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);085intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,086RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);087intent.putExtra(RecognizerIntent.EXTRA_PROMPT,Speech recognition demo);088startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);089090091/*092* Handle the results from the recognition activity.093*/094Override095protectedvoidonActivityResult(intrequestCode,intresultCode, Intent data) 096if(requestCode = VOICE_RECOGNITION_REQUEST_CODE & resultCode = RESULT_OK) 097/ Fill the list view with the strings the recognizer thought it could have heard098ArrayList matches = data.getStringArrayListExtra(099RecognizerIntent.EXTRA_

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论