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
< ResolveInfo >  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 < String >  matches  =  data.getStringArrayListExtra(
                    RecognizerIntent.EXTRA_RESULTS);
            mList.setAdapter(
new  ArrayAdapter < String > ( this , android.R.layout.simple_list_item_1,
                    matches));
        }

        
super .onActivityResult(requestCode, resultCode, data);
    }
}

 

 

Tip:本篇 从Api Demo直接复制过来,必须有网络支持。






 本文转自 terry_龙 51CTO博客,原文链接:http://blog.51cto.com/terryblog/421562,如需转载请自行联系原作者


相关实践学习
一键创建和部署高分电影推荐语音技能
本场景使用天猫精灵技能应用平台提供的技能模板,在2-5分钟内,创建一个好玩的高分电影推荐技能,使用模板后无须代码开发,系统自动配置意图、实体等,新手0基础也可体验创建技能的乐趣。
达摩院智能语音交互 - 声纹识别技术
声纹识别是基于每个发音人的发音器官构造不同,识别当前发音人的身份。按照任务具体分为两种: 声纹辨认:从说话人集合中判别出测试语音所属的说话人,为多选一的问题 声纹确认:判断测试语音是否由目标说话人所说,是二选一的问题(是或者不是) 按照应用具体分为两种: 文本相关:要求使用者重复指定的话语,通常包含与训练信息相同的文本(精度较高,适合当前应用模式) 文本无关:对使用者发音内容和语言没有要求,受信道环境影响比较大,精度不高 本课程主要介绍声纹识别的原型技术、系统架构及应用案例等。 讲师介绍: 郑斯奇,达摩院算法专家,毕业于美国哈佛大学,研究方向包括声纹识别、性别、年龄、语种识别等。致力于推动端侧声纹与个性化技术的研究和大规模应用。
相关文章
|
Java 语音技术 开发工具
Android 讯飞离线语音听写/离线语音识别SDK
Android 讯飞离线语音听写/离线语音识别SDK
388 0
Android 讯飞离线语音听写/离线语音识别SDK
|
Android开发
flutter中实现仿Android端的onResume和onPause方法
flutter中实现仿Android端的onResume和onPause方法
|
4月前
|
JSON 自然语言处理 Java
Android App开发语音处理之系统自带的语音引擎、文字转语音、语音识别的讲解及实战(超详细 附源码)
Android App开发语音处理之系统自带的语音引擎、文字转语音、语音识别的讲解及实战(超详细 附源码)
116 0
|
4月前
|
JSON 语音技术 Android开发
【Android App】在线语音识别功能实现(使用云知声平台与WebSocket 超详细 附源码)
【Android App】在线语音识别功能实现(使用云知声平台与WebSocket 超详细 附源码)
34 0
|
4月前
|
XML Java 语音技术
Android App开发在线语音识别处理中实现中文转拼音(Pinyin4j库)功能(超详细 附源码和演示)
Android App开发在线语音识别处理中实现中文转拼音(Pinyin4j库)功能(超详细 附源码和演示)
61 0
|
Android开发 容器
Android实现面包屑效果,支持Fragment联动
Android实现面包屑效果,支持Fragment联动
|
Android开发
Android实现连线题效果
Android实现连线题效果
|
Android开发
Android实现调用系统相机录像及实现录音
Android实现调用系统相机录像及实现录音
581 0
|
移动开发 JavaScript Android开发
通过howler.js实现在Android下的微信浏览器自动播放音频
通过howler.js实现在Android下的微信浏览器自动播放音频
399 0
通过howler.js实现在Android下的微信浏览器自动播放音频
|
存储 Dart Java
【Flutter】packages思维以及使用Java添加Android平台特定的实现在Flutter框架里的体现和运用
【Flutter】packages思维以及使用Java添加Android平台特定的实现在Flutter框架里的体现和运用