Android 轻松实现语音识别的完整代码

001

* Copyright (C)2008The Android Open Source Project

002

*


003

* Licensed under the Apache License, Version2.0(the"License");

004

* you may not usethisfile except in compliance with the License.


005

* You may obtain a copy of the License at

006

*


007

* http://www.apache.org/licenses/LICENSE-2.0

008

*


009

* Unless required by applicable law or agreed to in writing, software

010

* distributed under the License is distributed on an"AS IS"BASIS,


011

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

012

* See the Licenseforthe specific language governing permissions and


013

* limitations under the License.

014

*/


015

016

packagecom.example.android.apis.app;


017

018

importcom.example.android.apis.R;


019

020

importandroid.app.Activity;


021

importandroid.content.Intent;

022

importandroid.content.pm.PackageManager;


023

importandroid.content.pm.ResolveInfo;

024

importandroid.os.Bundle;


025

importandroid.speech.RecognizerIntent;

026

importandroid.view.View;


027

importandroid.view.View.OnClickListener;

028

importandroid.widget.ArrayAdapter;


029

importandroid.widget.Button;

030

importandroid.widget.ListView;


031

032

importjava.util.ArrayList;


033

importjava.util.List;

034


035

/**

036

* Sample code that invokes the speech recognition intent API.


037

*/

038

publicclassVoiceRecognitionextendsActivityimplementsOnClickListener {


039

040

privatestaticfinalintVOICE_RECOGNITION_REQUEST_CODE =1234;


041

042

privateListView mList;


043

044

/**


045

* Called with the activity is first created.

046

*/


047

@Override

048

publicvoidonCreate(Bundle savedInstanceState) {


049

super.onCreate(savedInstanceState);

050


051

// Inflate our UI from its XML layout description.

052

setContentView(R.layout.voice_recognition);


053

054

// Get display items for later interaction


055

Button speakButton = (Button) findViewById(R.id.btn_speak);

056


057

mList = (ListView) findViewById(R.id.list);

058


059

// Check to see if a recognition activity is present

060

PackageManager pm = getPackageManager();


061

List activities = pm.queryIntentActivities(

062

newIntent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH),0);


063

if(activities.size() !=0) {

064

speakButton.setOnClickListener(this);


065

}else{

066

speakButton.setEnabled(false);


067

speakButton.setText("Recognizer not present");

068

}


069

}

070


071

/**

072

* Handle the click on the start recognition button.


073

*/

074

publicvoidonClick(View v) {


075

if(v.getId() == R.id.btn_speak) {

076

startVoiceRecognitionActivity();


077

}

078

}


079

080

/**


081

* Fire an intent to start the speech recognition activity.

082

*/


083

privatevoidstartVoiceRecognitionActivity() {

084

Intent intent =newIntent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);


085

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,

086

RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);


087

intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"Speech recognition demo");

088

startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);


089

}

090


091

/**

092

* Handle the results from the recognition activity.


093

*/

094

@Override


095

protectedvoidonActivityResult(intrequestCode,intresultCode, Intent data) {

096

if(requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {


097

// Fill the list view with the strings the recognizer thought it could have heard

098

ArrayList matches = data.getStringArrayListExtra(


099

RecognizerIntent.EXTRA_RESULTS);

100

mList.setAdapter(newArrayAdapter(this, android.R.layout.simple_list_item_1,


101

matches));

102

}


103

104

super.onActivityResult(requestCode, resultCode, data);


105

}

106

}

更多相关文章

  1. Android(安卓)2.3的camera的虚拟对焦的去除
  2. 代码中如何设置TextView为不可见
  3. Android(安卓)轻松实现语音识别的完整代码
  4. Android桌面小部件实例 桌面小时钟
  5. android 系统关机,重启
  6. Android(安卓)BLE Gatt实现原理解析(未完)
  7. android 自定义ListView中的单项选择
  8. Android桌面小部件实例 桌面小时钟
  9. android bitmap 处理

随机推荐

  1. Lottie-移动动画效果框架
  2. Android(安卓)源码编译make的错误处理
  3. android中webview开启了硬件加速后会出现
  4. android上传图片到服务器,求服务器那边和a
  5. android实用代码
  6. Arcgis for android 离线查询
  7. Android(安卓)Studio 3.0 Canary 8无法安
  8. ionic爬过的各种坑;(持续更新)
  9. Android通过onDraw实现在View中绘图操作
  10. Android通过openGL实现视频贴纸功能