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

 

 

1.     说明
以下例程功能为:在应用程序中使用通于访问service调用语言识别功能,录音并识别后将识别的字串通过Listener返回给应用程序。注意:使用前需要安装语音识别服务,如编译安装源码中的development/samples/VoiceRecogitionService

2.     本例参考自android源码

a)          后台服务
参见development/samples/VoiceRecognitionService/*
此处实现了一个模拟的后台服务它并未实现真的语音识别而只是一个框架以示例编译并安装它即可在设置的语音输入与输出中看到它它包含了一个设置界面当连接这个Service如果设置了Letters则直接返回abc如果设置了Numbers则直接返回
123
你可以自己实现用于连接android源码自带的识别引擎srec.

b)         前台程序
参见frameworks/base/core/java/android/speech/Recognition*
与后台Service交互此段代码实现在应用程序界面中

3.     可从此处下载可独立运行的代码(前台程序)
http://download.csdn.net/source/2591401

4.     核心代码及说明

package com.android.mystt3;

 

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.speech.RecognitionListener;

import android.speech.RecognizerIntent;

import android.speech.SpeechRecognizer;

import android.widget.Button;

import android.widget.TextView;

import java.util.ArrayList;

import android.util.Log;

 

public class MyStt3Activity extends Activity implements OnClickListener {

       private TextView mText;

       private SpeechRecognizer sr;

       private static final String TAG = "MyStt3Activity";

 

       @Override

       public void onCreate(Bundle savedInstanceState) {

                super.onCreate(savedInstanceState);

                setContentView(R.layout.main);

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

                mText = (TextView) findViewById(R.id.text);      // 显示识别字串

                speakButton.setOnClickListener(this);

                sr = SpeechRecognizer.createSpeechRecognizer(this);        // 初始化识别工具,得到句柄

                sr.setRecognitionListener(new listener());         // 注册回调类及函数

       }

 

       class listener implements RecognitionListener            // 回调类的实现

       {

                public void onReadyForSpeech(Bundle params)

                {

                         Log.d(TAG, "onReadyForSpeech");

                }

                public void onBeginningOfSpeech()

                {

                         Log.d(TAG, "onBeginningOfSpeech");

                }

                public void onRmsChanged(float rmsdB)

                {

                         Log.d(TAG, "onRmsChanged");

                }

                public void onBufferReceived(byte[] buffer)

                {

                         Log.d(TAG, "onBufferReceived");

                }

                public void onEndOfSpeech()

                {

                         Log.d(TAG, "onEndofSpeech");

                }

                public void onError(int error)

                {

                         Log.d(TAG,  "error " +  error);

                         mText.setText("error " + error);

                }

                public void onResults(Bundle results)     // 返回识别到的数据

                {

                         String str = new String();

                         Log.d(TAG, "onResults " + results);

                         ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

                         for (int i = 0; i < data.size(); i++)

                         {

                                   Log.d(TAG, "result " + data.get(i));

                                   str += data.get(i);

                         }

                         mText.setText(str);        // 显示被识别的数据

                }

                public void onPartialResults(Bundle partialResults)

                {

                         Log.d(TAG, "onPartialResults");

                }

                public void onEvent(int eventType, Bundle params)

                {

                         Log.d(TAG, "onEvent " + eventType);

                }

       }

 

       public void onClick(View v) {

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

                         sr.startListening(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH));

                }

       }

}

 

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

 

posted on   xieyan0811  阅读(23)  评论(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工具
点击右上角即可分享
微信分享提示