Android 轻松实现语音识别

Android 轻松实现语音识别

老枪发布于2010年12月17日8时, 56评/71680阅

分享到:

收藏+236

踩顶0

苹果的iphone 有语音识别用的是Google 的技术,做为Google 力推的Android 自然会将其核心技术往Android 系统里面植入,并结合google 的云端技术将其发扬光大。

所以Google Voice Recognition在Android 的实现就变得极其轻松。

语音识别,借助于云端技术可以识别用户的语音输入,包括语音控制等技术,下面我们将利用Google 提供的Api 实现这一功能。

功能点为:通过用户语音将用户输入的语音识别出来,并打印在列表上。

功能界面如下:

用户通过点击speak按钮显示界面:

用户说完话后,将提交到云端搜索:

在云端搜索完成后,返回打印数据:

3 4 5 6 7 8 9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45 * Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

* https://www.360docs.net/doc/3411473314.html,/licenses/LICENSE-2.0

*

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

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

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and

* limitations under the License.

*/

package com.example.android.apis.app;

import com.example.android.apis.R;

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;

/**

* Sample code that invokes the speech recognition intent API.

*/

public class VoiceRecognition extends Activity implements OnClickListener { private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;

private ListView mList;

/**

* Called with the activity is first created.

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89 @Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// Inflate our UI from its XML layout description.

setContentView(https://www.360docs.net/doc/3411473314.html,yout.voice_recognition);

// Get display items for later interaction

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

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

// Check to see if a recognition activity is present

PackageManager pm = getPackageManager();

List activities = pm.queryIntentActivities(

new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);

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

speakButton.setOnClickListener(this);

} else {

speakButton.setEnabled(false);

speakButton.setText("Recognizer not present");

}

}

/**

* Handle the click on the start recognition button.

*/

public void onClick(View v) {

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

startVoiceRecognitionActivity();

}

}

/**

* Fire an intent to start the speech recognition activity.

*/

private void startVoiceRecognitionActivity() {

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,

https://www.360docs.net/doc/3411473314.html,NGUAGE_MODEL_FREE_FORM);

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

}

91

92

93

94

95

96

97

98

99 100 101 102 103 104 105 106 /**

* Handle the results from the recognition activity.

*/

@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, https://www.360docs.net/doc/3411473314.html,yout.simple_list_item_1, matches));

}

super.onActivityResult(requestCode, resultCode, data);

}

}

Android:使用Speech To Text API进行语音到文本转换【南京·10月17日】OSC源创会开始报名:Swift、大型移动项目构架分享?

Android有一个非常酷的特性很多开发者都还不知道。Any.DO之类应用的语音到文本转换功能很有创意。在现在Siri的世界里,语音指令是极其重要的。Android原生提供Speech To Text功能,为什么不把它用在我们的程序中!

我将会展示如何在程序中使用Android的Speech To Text API,现在开始写我们的demo 程序。

Demo程序

这个程序很简单。他有一个Mic符号按钮。点击之后我们触发Android的Speech To Text 意图(Intent)显示一个对话框来接收语音输入。输入的语音然后会被转换成文本并显示到一个text view中。

第一步:在Eclipse中创建基本的Android项目

在Eclipse中创建一个Hello World Android项目。打开New > Project > Android Project,项目名填SpeechToTextDemo,选择Android运行时2.1或sdk7。我给定了包名:net.viralpatel.android.speechtotextdemo

做完上面的步骤,你就有了一个基本的Android Hello World程序

第二步:更改布局

在我们的demo中布局很简单。只有一个图像按钮来触发Speech to Text API和一个TextView来显示从语音转换过来的文本。

打开layout/main.xml并替换为下面的内容:

File: res/layout/main.xml

?

1 2 3 4 5 6 7 8 9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28 29

第三步:触发Speech to Text API的Android Java代码打开SpeechToTextDemoActivity 类并替换为下面的代码:File: SpeechT oTextDemoActivity.java

?

1

2

3 4 5 6 7 8

9

Android Speech to text Android API的核心是包android.speech和类android.speech.RecognizerIntent。我们触发一个意图(android.speech.RecognizerIntent)显示对话框来识别语音输入,这个Activity转换语音为文本并把结果传回我们正在调用的Activity。当我们调用android.speech.RecognizerIntent意图时,必须使用startActivityForResult()来接听文本结果。

注意在上面的代码中我们是怎样创建并触发意图intent android.speech.RecognizerIntent的,同时使用.putExtra()方法添加了一个参数。调用RecognizerIntent时,必须提供RecognizerIntent.EXTRA_LANGUAGE_MODE,在这里我们设置为en-US。

由于我们的RecognizerIntent通过startActivityForResult()触发,我们重写了onActivityResult(int requestCode, int resultCode, Intent data)方法来处理结果数据。RecognizerIntent会把语音转换为文本并把结果通过键RecognizerIntent.EXTRA_RESULTS作为ArrayList传回来。只有RESULT_OK返回时才会出现。我们只需要使用txtText.setText()把从结果中拿到的文本设置到text view texText 中。

在这里值得注意的一件事是在不支持speech to text API的设备/Android版本中应该怎样处理。在这种情况下,当我们视图启动Activity时ActivityNotFoundException异常会被

抛出。在上面的例子中,我们捕获了这个异常并使用T oast显示了一个提示信息“Opps! Your device doesn’t support Speech to Text”。

Android应用程序的屏幕截图

到这里就结束了!在Android模拟器或真实设备上执行应用程序,将会看到下面的输出。

android语音识别技术

发表于4年前(2012-03-20 18:51) 阅读(842) | 评论(3)2人收藏此文章, 我要收藏

赞0

尖er货来了!亚马逊AWS AWSome Day 免费云计算培训开始报名

今天从网上找了个例子实现了语音识别,个人感觉挺好玩的,就把代码贴出来与大家分享下:

Android中主要通过RecognizerIntent来实现语音识别,其实代码比较简单,但是如果找不到设置,就会抛出异常ActivityNotFoundException,所以我们需要捕捉这个异常。而且语音识别在模拟器上是无法测试的,因为语音识别是访问google云端数据,所以如果手机的网络没有开启,就无法实现识别声音的!一定要开启手机的网络,如果手机不存在语音识别功能的话,也是无法启用识别!

下面是RecognizerIntentActivity中的代码:

源码打印?

详解Android SDK1.6中Text-To-Speech(TTS)语音朗读发表于6年前(2010-01-06 18:00) 阅读(4389) | 评论(2)4人收藏此文章, 我要收藏

赞3

尖er货来了!亚马逊AWS AWSome Day 免费云计算培训开始报名

TextToSpeech简称 TTS,是Android 1.6版本中比较重要的新功能。将所指定的文本转成不同语言音频输出。它可以方便的嵌入到游戏或者应用程序中,增强用户体验。

在讲解TTS API和将这项功能应用到你的实际项目中的方法之前,先对这套TTS引擎有个初步的了解。

对TTS资源的大体了解:

TTS engine依托于当前Android Platform所支持的几种主要的语言:English、French、German、Italian和Spanish五大语言(暂时没有我们伟大的中文,至少Google的科学家们还没有把中文玩到炉火纯青的地步,先易后难也是理所当然。)TTS可以将文本随意的转换成以上任意五种语言的语音输出。与此同时,对于个别的语言版本将取决于不同的时区,

例如:对于English,在TTS中可以分别输出美式和英式两种不同的版本(由此看出Googl e的做事风格真够细致,而正因为如此估计Google不加入中文的另外一种理由是中文的方言太多了)。

能支持如此庞大的数据量,TTS 引擎对于资源的优化采取预加载的方法。根据一系列的参数信息(参数的用法将在后边有详细的介绍)从库中提取相应的资源,并加载到当前系统中。尽管当前大部分加载有Android操作系统的设备都通过这套引擎来提供TTS功能,但由于一些设备的存储空间非常有限而影响到TTS无法最大限度的发挥功能,算是当前的一个瓶颈。为此,开发小组引入了检测模块,让利用这项技术的应用程序或者游戏针对于不同的设备可以有相应的优化调整,从而避免由于此项功能的限制,影响到整个应用程序的使用。比较稳妥的做法是让用户自行选择是否有足够的空间或者需求来加载此项资源,下边给出一个标准的检测方法:

Intent checkIntent = new Intent();

checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

如果当前系统允许创建一个“android.speech.tts.TextToSpeech”的Object, 说明已经提供TTS功能的支持,将检测返回结果中给出“CHECK_VOICE_DATA_PASS ”的标记。如果系统不支持这项功能,那么用户可以选择是否加载这项功能,从而让设备支持输出多国语言的语音功能“Multi-lingual Talking”。“ACTION_INSTALL_TTS_DATA”intent将用户引入Android market中的TTS下载界面。下载完成后将自动完成安装,下边是实现这一过程的完整代码(https://www.360docs.net/doc/3411473314.html,) :

private TextToSpeech mTts;

protected void onActivityResult(

int requestCode, int resultCode, Intent data) {

if (requestCode == MY_DATA_CHECK_CODE) {

if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {

// success, create the TTS instance

mTts = new TextToSpeech(this, this);

} else {

// missing data, install it

Intent installIntent = new Intent();

installIntent.setAction(

TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);

startActivity(installIntent);

}

}

}

TextToSpeech实体和OnInitListener都需要引用当前Activity的Context作为构造参数。OnInitListener()的用处是通知系统当前TTS Engine已经加载完成,并处于可用状态。根据需求设置语言参数:

早在Google I/O大会上,官方给出了一段关于应用这项功能的鲜活体验,将翻译结果直接通过五种不同国家语言的语音输出。加载语言的方法非常简单:

mTts.setLanguage(https://www.360docs.net/doc/3411473314.html,);

相关文档
最新文档