服务器之家

服务器之家 > 正文

Android实现语音识别代码

时间:2021-03-25 14:36     来源/作者:Android开发网

苹果的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);
}
}

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

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部