Android语音识别的具体实现实例

44 篇文章 1 订阅
3 篇文章 0 订阅

Android语音识别的具体实现实例

-------------------------------------------------------------------------------------------

Google语音识别

http://www.apkbus.com/forum.php?mod=viewthread&tid=3473

http://blog.csdn.net/wangkuifeng0118/article/details/7251813


本质上这三个实例的代码均来自Android源码

-------------------------------------------------------------------------------------------

Android语音识别方法一:使用intent调用语音识别程序

1.说明

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

2.本例参考自android例程:

development/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.java

3.核心代码及说明

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

Android语音识别方法二:应用程序自己调用语音识别库

1.说明

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

2.本例参考自android代码:

frameworks/base/core/java/android/speech/srec/Recognizer.java中注释部分

3.核心代码及说明

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

Android语音识别方法三:使用Service调用语音识别程序

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.核心代码及说明

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


About Wouldn't your prefer to let your users speak instead of making them type? This plugin uses OS components for speech recognition and send it to your Unity scripts as String objects. Plugin supports: - Android >= 3.0 (haven’t tested below, it might work though… ), - iOS >= 10.0. That doesn’t mean you can’t target iOS lower than 10 - you simply have to prepare fallback code to cover cases when user doesn’t have access to speech recognition (SpeechRecognizer.EngineExists() for the help!). Keep in mind that both iOS and Android might use Internet connection for speech detection, which means it might fail in case there’s no active connection. Plugin doesn’t work in Editor! You have to run your app on real iOS or Android device. MOBILE SPEECH RECOGNIZER - UNITY PLUGIN ?2 Quick Start Open example scene Go to KKSpeechRecognizer/Example folder inside Unity and open ExampleScene: It shows basic usage of a plugin, which is: 1. Detecting if speech recognition exists on user’s device (keep in mind that it won’t be available on e.g. iOS 9 or old Android phones), 2. If it exists, and user clicks on “Start Recording” button it listens for recognized text and displays it on a screen, 3. On Android, speech recognition automatically detects when user finishes speaking, but on iOS we have to wait for user clicking “Stop Recording” to finish whole process (i.e. get final results). Before running it on Android or iOS device you have to… Setup permissions iOS After generating Xcode project (keep in mind that you have to use Xcode 8 or higher) you have to add two permissions keys to your project: MOBILE SPEECH RECOGNIZER - UNITY PLUGIN ?3 NSMicrophoneUsageDescription explanation from Apple docs: This key lets you describe the reason your app accesses any of the the device’s microphones. When the system prompts the user to allow access, this string is displayed as part of the alert. NSSpeechRecognitionUsageDescription explanation from Apple docs: This key lets you describe the reason y
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值