随笔 - 383  文章 - 0  评论 - 0  阅读 - 35557 

 

 

 

1.     说明
以下例程功能为:在应用程序中使用intent来调出语言识别界面,录音并识别后将识别的字串返回给应用程序。注意:使用前需要安装语音识别程序如语音搜索。

2.     本例参考自android例程:
development/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.java

3.     可从此处下载可独立运行的代码:
http://download.csdn.net/source/2591401

4.     核心代码及说明

package com.android.mystt1;

 

import android.app.Activity;

import android.content.Intent;

import android.content.pm.PackageManager;

import android.content.pm.ResolveInfo;

import android.os.Bundle;

import android.speech.RecognizerIntent;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.ListView;

 

import java.util.ArrayList;

import java.util.List;

 

public class MyStt1Activity extends Activity implements OnClickListener {

       private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;

       private ListView mList;          // 显示识别后字串的list控件

 

       @Override

       public void onCreate(Bundle savedInstanceState) {

                super.onCreate(savedInstanceState);

                setContentView(R.layout.main);

                Button speakButton = (Button) findViewById(R.id.btn_speak); // 识别按钮

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

                PackageManager pm = getPackageManager();

                List activities = pm.queryIntentActivities(

                          new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); //本地识别程序

//                       new Intent(RecognizerIntent.ACTION_WEB_SEARCH), 0); // 网络识别程序

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

                         speakButton.setOnClickListener(this);

                } else {                 // 若检测不到语音识别程序在本机安装,测将扭铵置灰

                         speakButton.setEnabled(false);

                         speakButton.setText("Recognizer not present");

                }

       }

 

       public void onClick(View v) {

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

                         startMysttActivityActivity();

                }

       }

 

       private void startMysttActivityActivity() {          // 开始识别

                Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,

                                   RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

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

                startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
                //
调出识别界面

    }

 

       @Override

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

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

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

                         ArrayList matches = data.getStringArrayListExtra(

                                            RecognizerIntent.EXTRA_RESULTS);

                         mList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,

                                            matches));

                }

                // 语音识别后的回调,将识别的字串在list中显示

                super.onActivityResult(requestCode, resultCode, data);

       }

}

(转载请注明出处: http://xy0811.spaces.live.com/) 

posted on   xieyan0811  阅读(18)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示