Android 轻松实现语音识别

简介:
 

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

所以Google Voice Recognition在Android 的实现就变得极其轻松。
Android 轻松实现语音识别
语音识别,借助于云端技术可以识别用户的语音输入,包括语音控制等技术,下面我们将利用Google 提供的Api 实现这一功能。

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

功能界面如下:

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

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

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

 

Android 轻松实现语音识别的完整代码

 

Java代码 复制代码  收藏代码
  1. * Copyright (C) 2008 The Android Open Source Project   
  2. *   
  3. * Licensed under the Apache License, Version 2.0 (the "License");   
  4. * you may not use this file except in compliance with the License.   
  5. * You may obtain a copy of the License at   
  6. *   
  7. * http://www.apache.org/licenses/LICENSE-2.0   
  8. *   
  9. * Unless required by applicable law or agreed to in writing, software   
  10. * distributed under the License is distributed on an "AS IS" BASIS,   
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   
  12. * See the License for the specific language governing permissions and   
  13. * limitations under the License.   
  14. */   
  15.   
  16. package com.example.android.apis.app;   
  17.   
  18. import com.example.android.apis.R;   
  19.   
  20. import android.app.Activity;   
  21. import android.content.Intent;   
  22. import android.content.pm.PackageManager;   
  23. import android.content.pm.ResolveInfo;   
  24. import android.os.Bundle;   
  25. import android.speech.RecognizerIntent;   
  26. import android.view.View;   
  27. import android.view.View.OnClickListener;   
  28. import android.widget.ArrayAdapter;   
  29. import android.widget.Button;   
  30. import android.widget.ListView;   
  31.   
  32. import java.util.ArrayList;   
  33. import java.util.List;   
  34.   
  35. /**  
  36. * Sample code that invokes the speech recognition intent API.  
  37. */  
  38. public class VoiceRecognition extends Activity implements OnClickListener {   
  39.   
  40. private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;   
  41.   
  42. private ListView mList;   
  43.   
  44. /**  
  45. * Called with the activity is first created.  
  46. */  
  47. @Override  
  48. public void onCreate(Bundle savedInstanceState) {   
  49. super.onCreate(savedInstanceState);   
  50.   
  51. // Inflate our UI from its XML layout description.   
  52. setContentView(R.layout.voice_recognition);   
  53.   
  54. // Get display items for later interaction   
  55. Button speakButton = (Button) findViewById(R.id.btn_speak);   
  56.   
  57. mList = (ListView) findViewById(R.id.list);   
  58.   
  59. // Check to see if a recognition activity is present   
  60. PackageManager pm = getPackageManager();   
  61. List activities = pm.queryIntentActivities(   
  62. new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);   
  63. if (activities.size() != 0) {   
  64. speakButton.setOnClickListener(this);   
  65. else {   
  66. speakButton.setEnabled(false);   
  67. speakButton.setText("Recognizer not present");   
  68. }   
  69. }   
  70.   
  71. /**  
  72. * Handle the click on the start recognition button.  
  73. */  
  74. public void onClick(View v) {   
  75. if (v.getId() == R.id.btn_speak) {   
  76. startVoiceRecognitionActivity();   
  77. }   
  78. }   
  79.   
  80. /**  
  81. * Fire an intent to start the speech recognition activity.  
  82. */  
  83. private void startVoiceRecognitionActivity() {   
  84. Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);   
  85. intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,   
  86. RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);   
  87. intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");   
  88. startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);   
  89. }   
  90.   
  91. /**  
  92. * Handle the results from the recognition activity.  
  93. */  
  94. @Override  
  95. protected void onActivityResult(int requestCode, int resultCode, Intent data) {   
  96. if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {   
  97. // Fill the list view with the strings the recognizer thought it could have heard   
  98. ArrayList matches = data.getStringArrayListExtra(   
  99. RecognizerIntent.EXTRA_RESULTS);   
  100. mList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,   
  101. matches));   
  102. }   
  103.   
  104. super.onActivityResult(requestCode, resultCode, data);   
  105. }   
  106. }   
相关实践学习
达摩院智能语音交互 - 声纹识别技术
声纹识别是基于每个发音人的发音器官构造不同,识别当前发音人的身份。按照任务具体分为两种: 声纹辨认:从说话人集合中判别出测试语音所属的说话人,为多选一的问题 声纹确认:判断测试语音是否由目标说话人所说,是二选一的问题(是或者不是) 按照应用具体分为两种: 文本相关:要求使用者重复指定的话语,通常包含与训练信息相同的文本(精度较高,适合当前应用模式) 文本无关:对使用者发音内容和语言没有要求,受信道环境影响比较大,精度不高 本课程主要介绍声纹识别的原型技术、系统架构及应用案例等。 讲师介绍: 郑斯奇,达摩院算法专家,毕业于美国哈佛大学,研究方向包括声纹识别、性别、年龄、语种识别等。致力于推动端侧声纹与个性化技术的研究和大规模应用。
相关文章
|
机器学习/深度学习 算法 openCL
高效、轻量的深度学习框架MNN
MNN是一个高效、轻量的深度学习框架。
高效、轻量的深度学习框架MNN
一个免费功能强大的谷歌翻译api
分享一个免费且功能强大谷歌翻译api
11324 2
一个免费功能强大的谷歌翻译api
|
Ubuntu 数据安全/隐私保护
Ubuntu下/etc/sudoers的设置和sudo免密码执行及设置无效的原因
Ubuntu下免密码执行sudo及设置无效的原因
4303 0
|
编解码 安全 搜索推荐
还没适配 Android 12 的要抓紧了(下)
还没适配 Android 12 的要抓紧了(下)
2639 0
还没适配 Android 12 的要抓紧了(下)
|
Linux Android开发 编解码
VLC播放RTSP视频延迟问题
之前写过一篇关于在Linux平台上编译Android平台上VLC播放器源代码的文章,vlc这款播放器非常优秀而且是开源的,它的核心是开源视频编解码库ffmpeg。而且这款播放器还支持RTSP协议,这个主要是用开源的live555来实现的,live555这个库以后还需要认真研习。
6477 0
|
安全 Shell Linux
ssh密码忘记了怎么办
通过上述措施,不仅能够有效应对SSH密码遗忘的挑战,还能全方位加固SSH连接的安全,确保数据传输的无忧。
1218 2
|
测试技术 开发工具 文件存储
Git Stash
【8月更文挑战第27天】
777 6
|
弹性计算 搜索推荐 安全
如何编写有效的Prompt模板:提升大模型性能的关键
在大模型应用中,编写有效的Prompt至关重要。本文介绍了如何编写高质量的Prompt模板,包括明确任务定义、选择高质量示例、优化任务指示和调整示例顺序。详细探讨了百炼平台提供的三种主要Prompt模板(ICIO、CRISPE、RASCEF)及静态和动态样例库的创建与应用,帮助提升模型性能。
1263 0
|
XML Java Android开发
Android App开发手机阅读中贝塞尔曲线的原理讲解及实现波浪起伏动画实战(附源码和演示视频 可直接使用)
Android App开发手机阅读中贝塞尔曲线的原理讲解及实现波浪起伏动画实战(附源码和演示视频 可直接使用)
460 0
|
机器学习/深度学习 数据采集 算法
学习笔记: 机器学习经典算法-主成分分析PCA与梯度上升法
机器学习经典算法-个人笔记和学习心得分享
523 0

热门文章

最新文章

下一篇
开通oss服务