2010-11-12 17:01:51

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://terryblog.blog.51cto.com/1764499/421562

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

    }

}

Android 轻松实现语音识别的更多相关文章

  1. 轻松实现语音识别的完整代码在android开发中

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

  2. Android 调用谷歌语音识别

    調用谷歌语音识别其实很简单,直接利用 intent 跳转到手机里面的谷歌搜索 代码也很简单,直接调用方法 startVoiceRecognitionActivity() 如果大家手机里面没有谷歌搜索, ...

  3. Android开发之语音识别

    2013-07-03 语音识别 2008年Google语音搜索在iphone平台上线,Android 1.5 将语音识别应用到搜索功能上. 手动输入是目前主要与手机互动的方式,语音搜索宗旨是最大限度地 ...

  4. Android使用百度语音识别api代码实现。

    第一步 ① 创建平台应用 点击百度智能云进入,没有账号的可以先注册账号,这里默认都有账号了,然后登录. 然后左侧导航栏点击找到语音技术 然后会进入一个应用总览页面, 然后点击创建应用 立即创建 点击查 ...

  5. Android 轻松实现仿淘宝地区选择

    介绍 最近用淘宝客户端的时候,编辑地址的时候有个地区选择的功能.看上面的效果觉得挺酷,滚动的时候,是最后一个从下面飞上来挨着前一个.就自己鼓捣一个出来玩玩. 说了效果可能不太直观,下面上两张图看看效果 ...

  6. android 讯飞语音识别(离线)注意事项

    讯飞语音识别:使用注意事项:mainfest.xml中一定要记得权限写进去21001:我的情况是没有写SpeechApp类,并且需要在application中注册:20005:无匹配结果23300:本 ...

  7. TitleLayout——一个Android轻松实现标题栏的库

    TitleLayout 多功能.通用的.可在布局或者使用Java代码实现标题栏: 支持沉浸式状态栏: 支持左侧返回按钮不需要手动实现页面返回: 支持左侧按钮,中间标题,右边按钮点击 左侧支持图片+文字 ...

  8. TitleLayout——一个Android轻松实现通用、标准、支持沉浸式状态栏的标题栏库

    TitleLayout 多功能.通用的.可在布局或者使用Java代码实现标题栏:支持沉浸式状态栏,支持左侧返回按钮(不需要手动实现页面返回),左侧支持图片+文字.图片.文字:右侧支持图片.文字等. 堆 ...

  9. Android 轻松实现后台搭建+APP版本更新

    http://blog.csdn.net/u012422829/article/details/46355515 (本文讲解了在Android中实现APP版本更新,文末附有源码.) 看完本文,您可以学 ...

随机推荐

  1. tensorflow 之常见模块conv,bn...实现

    使用tensorflow时,会发现tf.nn,tf.layers, tf.contrib模块有很多功能是重复的,尤其是卷积操作,在使用的时候,我们可以根据需要现在不同的模块.但有些时候可以一起混用. ...

  2. 以ScaleIO 1.30为后端存储运行微软服务器软件SQL Server 2014, SharePoint 2013, Exchange 2013的解决方案

    EMC新发布了以ScaleIO 1.30为后端存储来运行SQL, SharePoint, Exchange的解决方案白皮书.   下面的页面中有简要的介绍和整篇文档PDF的下载. https://co ...

  3. activity 保存数据

    activity 保存数据对android的商业项目十分的重要,譬如你在发微博的时候,突然来了一个电话,你洋洋洒洒写了100个字,你不能保存的话,你岂不要卖要骂娘. 那activity究竟是保存数据的 ...

  4. MongoDB学习笔记(六)--复制集+sharding分片 && 总结

    复制集+sharding分片                                                               背景 主机 IP 服务及端口 Server A ...

  5. 【LeetCode】Path Sum II 二叉树递归

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  6. 微信小程序中用户唯一ID的获取

    折腾到半夜,搞得挺兴奋,总结一下,免得忘了: 1.微信小程序直接获得的是一些简单信息,基本无用 2.用户唯一标识是openid,还有一个unionid是关联多个公众号之类情况下用,我不大关心 3.在g ...

  7. sublime同步侧边栏与窗体的颜色

    Ctrl+Shift+P -> install -> 搜索安装包 SyncedSidebarBg,自动同步侧边栏底色为编辑窗口底色. P.S. 有时改完后侧边栏颜色没变化,不知什么原因,打 ...

  8. -Prefix.pch has been modified 的错误修复

    方法一: 选择 Product > Clean 然后重新编译--运行: 方法二: 找到-Prefix.pch文件,把中间的 #ifdef __OBJC__ #import <UIKit/U ...

  9. UIScrollView 图片循环滚动

    1:假如有6个图片:那个,Scrollview的大小加 7 个图片的大小 2: //ImageScrollView; UIScrollView *imageScroll = [[UIScrollVie ...

  10. Python3.2官方文档翻译--标准库概览(一)

    7.1 操作系统接口 Os模块提供主要很多与操作系统交互的函数. >>> import os >>> os.getcwd() # Return the curren ...