Android实现语音识别代码
作者:hebedich 发布时间:2022-06-03 03:26:33
标签:Android,语音识别
苹果的iphone 有语音识别用的是Google 的技术,做为Google 力推的Android 自然会将其核心技术往Android 系统里面植入,并结合google 的云端技术将其发扬光大。 所以Google Voice Recognition在Android 的实现就变得极其轻松。
语音识别,借助于云端技术可以识别用户的语音输入,包括语音控制等技术,下面我们将利用Google 提供的Api 实现这一功能。 功能点为:通过用户语音将用户输入的语音识别出来,并打印在列表上。 功能界面如下:
用户通过点击speak按钮显示界面:
用户说完话后,将提交到云端搜索:
在云端搜索完成后,返回打印数据:
* 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);
}
}
以上所述就是本文的全部内容了,希望大家能够喜欢。


猜你喜欢
- 库从本质上来说是一种可执行代码的二进制格式,可以被载入内存中执行。库分静态库和动态库两种。 一、静态库和动态库的区别1. 静态函数库这类库的
- 本文实例讲述了C#对二进制数据进行base64编码的方法。分享给大家供大家参考。具体实现方法如下:using System;using Sy
- 本文实例为大家分享了Flutter自定义圆盘取色器的具体代码,供大家参考,具体内容如下下面展示一些 内联代码片。圆盘取色器效果图完整代码im
- MyBatis-Spring允许你在Service Bean中注入映射器。当使用映射器时,就像调用DAO那样来调用映射器就可以了,但是此时你
- 本文实例为大家分享了C#以流方式读socket超时设置的具体代码,供大家参考,具体内容如下using System;using System
- 定时器问题定时器属于基本的基础组件,不管是用户空间的程序开发,还是内核空间的程序开发,很多时候都需要有定时器作为基础组件的支持。一个定时器的
- 旋转扭曲特效是指在一个圆形区域内扭曲所渲染的图像,其他像素的旋转程度随着距离的变化而变化。具体可以通过修改Shader来实现。原始图片扭曲图
- 引言本文收集了我在看Lucene源码中遇到的所有的对单值(int,long,float,double)的压缩算法,可能一种类型针对不同的场景
- 最近看代码,由于代码的调用层级深度比较多,层层深入到某处时,已经忘记了身处何处,虽然自己可以使用一些画图工具来时序图,但是,这种情况下,自己
- 问题描述需要定时通过websocket接口来推送mysql里面最新的数据,自定义了定时器@Component@Slf4jpublic cla
- 一、项目简述本系统功能包括:数据统计、收件录入、发件录入、到件录入、派件录入、问题件录入、退件录入、留仓录入、装车录入、发车录入、到车录入、
- 实例如下:using System;using System.Linq.Expressions;class DynamicPredicate
- 本文实例讲述了Android自动朗读TTS用法。分享给大家供大家参考,具体如下:TextToSpeech简称 TTS,是自Android 1
- 这篇文章主要介绍了java的package和import机制原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习
- 本文实例为大家分享了Java实现扑克牌程序的具体代码,供大家参考,具体内容如下思路:在实现之前,先要想好步骤,思路清晰才不会出错。要实现一副
- 反射方式获取JPA Entity属性和值在记录日志或者调试的时候,往往需要输出数据库查询或者写入的值,或者在接 * 互的时候,可能需要将实体转
- 本文实例为大家分享了AndroidStudio实现能在图片上涂鸦的具体代码,供大家参考,具体内容如下一、内容:设计一个能在图片上涂鸦的程序二
- Android API Demos中有很多非常Nice的例子,这些例子的代码都写的很出色,如果大家把API Demos中的每个例子研究透了,
- 本文以新建的CUDA的.cu程序来进行说明,同样也适用于C程序。一,发现问题1,首先我们在vs2019中创建了工程以后(我所创建的工程名称为
- 我们在SpringBoot和MyBatis整合的时候,需要在SpringBoot中通过注解方式配置事务回滚1 Pojo类package co