服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android实现语音识别代码

Android实现语音识别代码

2021-03-25 14:36Android开发网 Android

语音识别在android上使用起来很方便也很简单.但是有个前提条件,就是android机器上必须预先安装google的语音搜索工具,今天我们就来详细探讨下

苹果的iphone 有语音识别用的是google 的技术,做为google 力推的android 自然会将其核心技术往android 系统里面植入,并结合google 的云端技术将其发扬光大。 所以google voice recognition在android 的实现就变得极其轻松。

Android实现语音识别代码

语音识别,借助于云端技术可以识别用户的语音输入,包括语音控制等技术,下面我们将利用google 提供的api 实现这一功能。 功能点为:通过用户语音将用户输入的语音识别出来,并打印在列表上。 功能界面如下:

Android实现语音识别代码

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

Android实现语音识别代码

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

Android实现语音识别代码

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

Android实现语音识别代码

?
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
* copyright (c) 2008 the android open source project
*
* 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
*
* http://www.apache.org/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.
*/
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
 
// inflate our ui from its xml layout description.
setcontentview(r.layout.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,
recognizerintent.language_model_free_form);
intent.putextra(recognizerintent.extra_prompt, "speech recognition demo");
startactivityforresult(intent, voice_recognition_request_code);
}
 
/**
* 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, android.r.layout.simple_list_item_1,
matches));
}
 
super.onactivityresult(requestcode, resultcode, data);
}
}

以上所述就是本文的全部内容了,希望大家能够喜欢。

延伸 · 阅读

精彩推荐