1.     说明
以下例程功能为:应用程序自身调用语言识别函数,程序以循环方式等待录音并识别后的字串。

2.     本例参考自android代码:
frameworks/base/core/java/android/speech/srec/Recognizer.java
中注释部分

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

4.     核心代码及说明

package com.android.mystt2;

 

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.widget.Button;

import android.widget.TextView;

import android.view.View;

import android.view.View.OnClickListener;

 

import android.speech.srec.Recognizer;

import android.speech.srec.MicrophoneInputStream;

import java.io.InputStream;

import java.io.IOException;

import android.util.Log;

 

public class MyStt2Activity extends Activity implements OnClickListener {

       private TextView mText;

       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);

       }

 

       public void onClick(View v) {

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

                         test();

                }

       }

 

       void test() {

                try {

                         InputStream audio = new MicrophoneInputStream(11025, 11025 * 5); //设置输入参数

                         String cdir = Recognizer.getConfigDir(null);    // 获取语音识别配置目录

                         Recognizer recognizer = new Recognizer(cdir + "/baseline11k.par");

                         Recognizer.Grammar grammar = recognizer.new Grammar(cdir

                                            + "/grammars/VoiceDialer.g2g");

                         grammar.setupRecognizer();

                         grammar.resetAllSlots();

                         grammar.compile();

                         recognizer.start();        // 开始识别

                         while (true) {       // 循环等待识别结果

                                   switch (recognizer.advance()) {

                                   case Recognizer.EVENT_INCOMPLETE:

                                   case Recognizer.EVENT_STARTED:

                                   case Recognizer.EVENT_START_OF_VOICING:

                                   case Recognizer.EVENT_END_OF_VOICING:

                                            continue;    // 未完成,继续等待识别结果

                                   case Recognizer.EVENT_RECOGNITION_RESULT:

                                            for (int i = 0; i < recognizer.getResultCount(); i++) {

                                                     String result = recognizer.getResult(i,

                                                                        Recognizer.KEY_LITERAL);

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

                                                     mText.setText(result);

                                            }        // 识别到字串,显示并退出循环

                                            break;

                                   case Recognizer.EVENT_NEED_MORE_AUDIO:

                                            recognizer.putAudio(audio)   // 需要更多音频数据;

                                            continue;

                                   default:

                                            break;

                                   }

                                   break;

                         }

                         recognizer.stop();

                         recognizer.destroy();

                         audio.close();      // 回收资源

                } catch (IOException e) {

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

                         mText.setText("error " + e);

                }

       }

}

 

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

 

posted on 2010-08-03 13:27  xieyan0811  阅读(13)  评论(0编辑  收藏  举报